File indexing completed on 2024-04-06 12:31:49
0001
0002
0003
0004
0005
0006
0007 #include "CatchAll.h"
0008 #include "clang/Basic/SourceManager.h"
0009 #include <clang/AST/Attr.h>
0010 #include "CmsSupport.h"
0011 using namespace clangcms;
0012 using namespace clang;
0013 using namespace ento;
0014 using namespace llvm;
0015
0016 void CatchAll::checkASTCodeBody(const clang::Decl* D,
0017 clang::ento::AnalysisManager& AM,
0018 clang::ento::BugReporter& BR) const {
0019 const char* sfile = BR.getSourceManager().getPresumedLoc(D->getLocation()).getFilename();
0020 if ((!sfile) || (!support::isCmsLocalFile(sfile)))
0021 return;
0022 if (D->hasAttr<CMSSaAllowAttr>())
0023 return;
0024 const clang::Stmt* s = D->getBody();
0025 if (!s)
0026 return;
0027 s = process(s);
0028 if (!s)
0029 return;
0030 clang::ento::LocationOrAnalysisDeclContext x(AM.getAnalysisDeclContext(D));
0031 clang::ento::PathDiagnosticLocation DLoc =
0032 clang::ento::PathDiagnosticLocation::createBegin(s, BR.getSourceManager(), x);
0033 BR.EmitBasicReport(D,
0034 this,
0035 "'catch(...)' in sources",
0036 "CMS code rules",
0037 "using 'catch(...)' is forbidden",
0038 DLoc,
0039 s->getSourceRange());
0040 }
0041
0042 const clang::Stmt* CatchAll::process(const clang::Stmt* S) const {
0043 if (clang::AttributedStmt::classof(S)) {
0044 const clang::Stmt* np = nullptr;
0045 auto const* Node = static_cast<const clang::AttributedStmt*>(S);
0046 auto* SS = Node->getSubStmt();
0047 for (const auto* A : Node->getAttrs()) {
0048 if (clang::CXXTryStmt::classof(SS) && clang::CMSSaAllowAttr::classof(A)) {
0049 return np;
0050 }
0051 }
0052 }
0053
0054 if (clang::CXXCatchStmt::classof(S) && checkCatchAll(static_cast<const clang::CXXCatchStmt*>(S)))
0055 return S;
0056 clang::Stmt::const_child_iterator b = S->child_begin();
0057 clang::Stmt::const_child_iterator e = S->child_end();
0058 const clang::Stmt* catchAll = nullptr;
0059 while (b != e) {
0060 if (*b) {
0061 catchAll = process(*b);
0062 if (catchAll != nullptr)
0063 break;
0064 }
0065 b++;
0066 }
0067 return catchAll;
0068 }