From 925a3b596920db23bd4ddde256959baf9fbd8e30 Mon Sep 17 00:00:00 2001 From: Sergio Martins Date: Tue, 28 Aug 2018 19:12:48 +0100 Subject: Add ability to ignore directories via command line For ClazyStandalone it's with --ignore-dirs. For clazy it's via the CLAZY_IGNORE_DIRS env variable. Both take a regexp, to make it similar to clang-tidy. BUG: 397885 --- src/Clazy.cpp | 19 ++++++++++--------- src/Clazy.h | 2 ++ src/ClazyContext.cpp | 5 ++++- src/ClazyContext.h | 36 +++++++++++++++++++++++++++++++----- src/ClazyStandaloneMain.cpp | 6 +++++- src/checkbase.cpp | 2 +- 6 files changed, 53 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/Clazy.cpp b/src/Clazy.cpp index d000ab4f..7b7dda18 100644 --- a/src/Clazy.cpp +++ b/src/Clazy.cpp @@ -189,11 +189,11 @@ std::unique_ptr ClazyASTAction::CreateASTConsumer(CompilerIn return std::unique_ptr(astConsumer.release()); } -static std::string getHeaderFilterEnv() +static std::string getEnvVariable(const char *name) { - const char *headerFilter = getenv("CLAZY_HEADER_FILTER"); - if (headerFilter) - return headerFilter; + const char *result = getenv(name); + if (result) + return result; else return std::string(); } @@ -205,7 +205,7 @@ bool ClazyASTAction::ParseArgs(const CompilerInstance &ci, const std::vector args = args_; if (parseArgument("help", args)) { - m_context = new ClazyContext(ci, getHeaderFilterEnv(), ClazyContext::ClazyOption_None); + m_context = new ClazyContext(ci, getEnvVariable("CLAZY_HEADER_FILTER"), getEnvVariable("CLAZY_IGNORE_DIRS"), ClazyContext::ClazyOption_None); PrintHelp(llvm::errs()); return true; } @@ -238,7 +238,7 @@ bool ClazyASTAction::ParseArgs(const CompilerInstance &ci, const std::vector ClazyStandaloneASTAction::CreateASTConsumer(CompilerInstance &ci, llvm::StringRef) { - auto context = new ClazyContext(ci, m_headerFilter, m_options); + auto context = new ClazyContext(ci, m_headerFilter, m_ignoreDirs, m_options); auto astConsumer = new ClazyASTConsumer(context); auto cm = CheckManager::instance(); diff --git a/src/Clazy.h b/src/Clazy.h index 12497f0d..b126e221 100644 --- a/src/Clazy.h +++ b/src/Clazy.h @@ -73,12 +73,14 @@ class ClazyStandaloneASTAction : public clang::ASTFrontendAction public: explicit ClazyStandaloneASTAction(const std::string &checkList, const std::string &headerFilter, + const std::string &ignoreDirs, ClazyContext::ClazyOptions = ClazyContext::ClazyOption_None); protected: std::unique_ptr CreateASTConsumer(clang::CompilerInstance &ci, llvm::StringRef) override; private: const std::string m_checkList; const std::string m_headerFilter; + const std::string m_ignoreDirs; const ClazyContext::ClazyOptions m_options; }; diff --git a/src/ClazyContext.cpp b/src/ClazyContext.cpp index b0467a49..4d4ecd82 100644 --- a/src/ClazyContext.cpp +++ b/src/ClazyContext.cpp @@ -50,7 +50,7 @@ public: }; ClazyContext::ClazyContext(const clang::CompilerInstance &compiler, - const string &headerFilter, ClazyOptions opts) + const string &headerFilter, const string &ignoreDirs, ClazyOptions opts) : ci(compiler) , astContext(ci.getASTContext()) , sm(ci.getSourceManager()) @@ -61,6 +61,9 @@ ClazyContext::ClazyContext(const clang::CompilerInstance &compiler, if (!headerFilter.empty()) headerFilterRegex = std::unique_ptr(new llvm::Regex(headerFilter)); + if (!ignoreDirs.empty()) + ignoreDirsRegex = std::unique_ptr(new llvm::Regex(ignoreDirs)); + const char *fixitsEnv = getenv("CLAZY_FIXIT"); allFixitsEnabled = (options & ClazyOption_AllFixitsEnabled); if (!allFixitsEnabled && fixitsEnv) { diff --git a/src/ClazyContext.h b/src/ClazyContext.h index dab85fef..608e86b4 100644 --- a/src/ClazyContext.h +++ b/src/ClazyContext.h @@ -67,6 +67,7 @@ public: explicit ClazyContext(const clang::CompilerInstance &ci, const std::string &headerFilter, + const std::string &ignoreDirs, ClazyOptions = ClazyOption_None); ~ClazyContext(); @@ -110,18 +111,42 @@ public: return clazy::contains(extraOptions, optionName); } - bool isHeaderFilteredOut(clang::SourceLocation loc) const + bool fileMatchesLoc(const std::unique_ptr ®ex, clang::SourceLocation loc, const clang::FileEntry **file) const { + if (!regex) + return false; + + if (!(*file)) { + clang::FileID fid = sm.getDecomposedExpansionLoc(loc).first; + *file = sm.getFileEntryForID(fid); + if (!(*file)) { + return false; + } + } + + StringRef fileName((*file)->getName()); + return regex->match(fileName); + } + + bool shouldIgnoreFile(clang::SourceLocation loc) const + { + // 1. Process the regexp that excludes files + const clang::FileEntry *file = nullptr; + if (ignoreDirsRegex) { + const bool matches = fileMatchesLoc(ignoreDirsRegex, loc, &file); + if (matches) + return true; + } + + // 2. Process the regexp that includes files. Has lower priority. if (!headerFilterRegex || isMainFile(loc)) return false; - clang::FileID fid = sm.getDecomposedExpansionLoc(loc).first; - const clang::FileEntry *file = sm.getFileEntryForID(fid); + const bool matches = fileMatchesLoc(headerFilterRegex, loc, &file); if (!file) return false; - StringRef fileName(file->getName()); - return !headerFilterRegex->match(fileName); + return !matches; } bool isMainFile(clang::SourceLocation loc) const @@ -156,6 +181,7 @@ public: clang::CXXMethodDecl *lastMethodDecl = nullptr; clang::Decl *lastDecl = nullptr; std::unique_ptr headerFilterRegex; + std::unique_ptr ignoreDirsRegex; }; #endif diff --git a/src/ClazyStandaloneMain.cpp b/src/ClazyStandaloneMain.cpp index 39fcd9ae..697091e6 100644 --- a/src/ClazyStandaloneMain.cpp +++ b/src/ClazyStandaloneMain.cpp @@ -65,6 +65,10 @@ from the main file of each translation unit are always displayed.)"), cl::init(""), cl::cat(s_clazyCategory)); +static cl::opt s_ignoreDirs("ignore-dirs", cl::desc(R"(Regular expression matching the names of the +directories for which diagnostics should never be emitted. Useful for ignoring 3rdparty code.)"), + cl::init(""), cl::cat(s_clazyCategory)); + static cl::extrahelp s_commonHelp(CommonOptionsParser::HelpMessage); class ClazyToolActionFactory : public clang::tooling::FrontendActionFactory @@ -97,7 +101,7 @@ public: if (s_ignoreIncludedFiles.getValue()) options |= ClazyContext::ClazyOption_IgnoreIncludedFiles; - return new ClazyStandaloneASTAction(s_checks.getValue(), s_headerFilter.getValue(), options); + return new ClazyStandaloneASTAction(s_checks.getValue(), s_headerFilter.getValue(), s_ignoreDirs.getValue(), options); } }; diff --git a/src/checkbase.cpp b/src/checkbase.cpp index cc64cd50..955411ce 100644 --- a/src/checkbase.cpp +++ b/src/checkbase.cpp @@ -152,7 +152,7 @@ void CheckBase::emitWarning(clang::SourceLocation loc, std::string error, if (m_context->suppressionManager.isSuppressed(m_name, loc, sm(), lo())) return; - if (m_context->isHeaderFilteredOut(loc)) + if (m_context->shouldIgnoreFile(loc)) return; if (loc.isMacroID()) { -- cgit v1.2.3