File indexing completed on 2024-04-06 12:31:51
0001
0002
0003
0004
0005
0006
0007 #include "UsingNamespace.h"
0008 #include "clang/Basic/SourceManager.h"
0009 #include "CmsSupport.h"
0010 using namespace clangcms;
0011
0012 void UsingNamespace::checkASTDecl(const clang::UsingDirectiveDecl *D,
0013 clang::ento::AnalysisManager &Mgr,
0014 clang::ento::BugReporter &BR) const {
0015 if (isDeclOK(D, BR))
0016 return;
0017 reportBug("'using namespace '", D, BR);
0018 }
0019
0020 void UsingNamespace::checkASTDecl(const clang::UsingDecl *D,
0021 clang::ento::AnalysisManager &Mgr,
0022 clang::ento::BugReporter &BR) const {
0023 if (isDeclOK(D, BR))
0024 return;
0025 std::string NS = D->getQualifier()->getAsNamespace()->getNameAsString();
0026 if (strcmp(NS.c_str(), "std") != 0)
0027 return;
0028 reportBug("'using std:: '", D, BR);
0029 }
0030
0031 bool UsingNamespace::isDeclOK(const clang::NamedDecl *D, clang::ento::BugReporter &BR) const {
0032 if (D->getDeclContext()->getParent() != nullptr)
0033 return true;
0034 const char *sfile = BR.getSourceManager().getPresumedLoc(D->getLocation()).getFilename();
0035 if (!support::isCmsLocalFile(sfile))
0036 return true;
0037 size_t flen = strlen(sfile);
0038 if ((sfile[flen - 2] != '.') || (sfile[flen - 1] != 'h'))
0039 return true;
0040 return false;
0041 }
0042
0043 void UsingNamespace::reportBug(const char *bug, const clang::Decl *D, clang::ento::BugReporter &BR) const {
0044 clang::ento::PathDiagnosticLocation DLoc = clang::ento::PathDiagnosticLocation::createBegin(D, BR.getSourceManager());
0045 std::string buf;
0046 llvm::raw_string_ostream os(buf);
0047 os << bug << " in headers files.";
0048 BR.EmitBasicReport(D, this, "using namespace in header files", "CMS code rules", os.str(), DLoc);
0049 }