aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSergio Martins <smartins@kde.org>2018-08-28 19:12:48 +0100
committerSergio Martins <smartins@kde.org>2018-09-08 18:16:49 +0100
commit925a3b596920db23bd4ddde256959baf9fbd8e30 (patch)
treefa206a1c42c9c32f178a631b0757d6cdb593b0d0 /src
parent46b1232867e605c81f0c1e49c61bef5cc91adddb (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/Clazy.cpp19
-rw-r--r--src/Clazy.h2
-rw-r--r--src/ClazyContext.cpp5
-rw-r--r--src/ClazyContext.h36
-rw-r--r--src/ClazyStandaloneMain.cpp6
-rw-r--r--src/checkbase.cpp2
6 files changed, 53 insertions, 17 deletions
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<clang::ASTConsumer> ClazyASTAction::CreateASTConsumer(CompilerIn
return std::unique_ptr<clang::ASTConsumer>(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<std
std::vector<std::string> 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<std
if (parseArgument("ignore-included-files", args))
m_options |= ClazyContext::ClazyOption_IgnoreIncludedFiles;
- m_context = new ClazyContext(ci, /*headerFilter=*/ "", m_options);
+ m_context = new ClazyContext(ci, /*headerFilter=*/ "", /*ignoreDirs=*/ "", m_options);
// This argument is for debugging purposes
const bool dbgPrintRequestedChecks = parseArgument("print-requested-checks", args);
@@ -343,18 +343,19 @@ void ClazyASTAction::PrintHelp(llvm::raw_ostream &ros) const
ros << "FixIts are experimental and rewrite your code therefore only one FixIt is allowed per build.\nSpecifying a list of different FixIts is not supported.\nBackup your code before running them.\n";
}
-ClazyStandaloneASTAction::ClazyStandaloneASTAction(const string &checkList, const string &headerFilter,
+ClazyStandaloneASTAction::ClazyStandaloneASTAction(const string &checkList, const string &headerFilter, const string &ignoreDirs,
ClazyContext::ClazyOptions options)
: clang::ASTFrontendAction()
, m_checkList(checkList.empty() ? "level1" : checkList)
- , m_headerFilter(headerFilter.empty() ? getHeaderFilterEnv() : headerFilter)
+ , m_headerFilter(headerFilter.empty() ? getEnvVariable("CLAZY_HEADER_FILTER") : headerFilter)
+ , m_ignoreDirs(ignoreDirs.empty() ? getEnvVariable("CLAZY_IGNORE_DIRS") : ignoreDirs)
, m_options(options)
{
}
unique_ptr<ASTConsumer> 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<clang::ASTConsumer> 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<llvm::Regex>(new llvm::Regex(headerFilter));
+ if (!ignoreDirs.empty())
+ ignoreDirsRegex = std::unique_ptr<llvm::Regex>(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<llvm::Regex> &regex, 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<llvm::Regex> headerFilterRegex;
+ std::unique_ptr<llvm::Regex> 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<std::string> 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()) {