aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Changelog1
-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
-rw-r--r--tests/clazy-standalone/config.json15
-rw-r--r--tests/clazy-standalone/fileToIgnore.cpp7
-rw-r--r--tests/clazy-standalone/fileToIgnore.cpp.expected2
-rw-r--r--tests/clazy-standalone/fileToNotIgnore.cpp6
-rw-r--r--tests/clazy-standalone/fileToNotIgnore.cpp.expected1
-rwxr-xr-xtests/run_tests.py7
13 files changed, 91 insertions, 18 deletions
diff --git a/Changelog b/Changelog
index 0ac27df5..e881c82b 100644
--- a/Changelog
+++ b/Changelog
@@ -81,6 +81,7 @@
- performance optimizations
- Added -header-filter=<regex> option to clazy-standalone. Only headers matching the regexp will have warnings,
besides the .cpp file from the translation unit, which is never filtered out.
+ - Added -ignore-dirs=<regex> option to clazy-standalone, and its CLAZY_IGNORE_DIRS env variable equivalent.
- Added CLAZY_HEADER_FILTER env variable which adds above functionality to both clazy and clazy-standalone
- unused-non-trivial-variable got unused-non-trivial-variable-no-whitelist option
- unused-non-trivial-variable got user-blacklist and user-whitelist support
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()) {
diff --git a/tests/clazy-standalone/config.json b/tests/clazy-standalone/config.json
index 18d2bb56..66d92f0c 100644
--- a/tests/clazy-standalone/config.json
+++ b/tests/clazy-standalone/config.json
@@ -10,6 +10,21 @@
"filename" : "header_filter2.cpp",
"header_filter" : ".*_foo.*.h",
"checks" : ["qdatetime-utc"]
+ },
+ {
+ "filename" : "header_filter2.cpp",
+ "header_filter" : ".*_foo.*.h",
+ "checks" : ["qdatetime-utc"]
+ },
+ {
+ "filename" : "fileToIgnore.cpp",
+ "ignore_dirs" : ".*fileToIgnore.*",
+ "checks" : ["qdatetime-utc"]
+ },
+ {
+ "filename" : "fileToNotIgnore.cpp",
+ "ignore_dirs" : ".*fileToIgnore.*",
+ "checks" : ["qdatetime-utc"]
}
]
}
diff --git a/tests/clazy-standalone/fileToIgnore.cpp b/tests/clazy-standalone/fileToIgnore.cpp
new file mode 100644
index 00000000..7fbbeb29
--- /dev/null
+++ b/tests/clazy-standalone/fileToIgnore.cpp
@@ -0,0 +1,7 @@
+#include "header_filter_foo.h"
+#include "header_filter_bar.h"
+
+void t()
+{
+ QDateTime::currentDateTime().toTime_t();
+}
diff --git a/tests/clazy-standalone/fileToIgnore.cpp.expected b/tests/clazy-standalone/fileToIgnore.cpp.expected
new file mode 100644
index 00000000..2dd58f65
--- /dev/null
+++ b/tests/clazy-standalone/fileToIgnore.cpp.expected
@@ -0,0 +1,2 @@
+clazy-standalone/header_filter_foo.h:5:5: warning: Use QDateTime::currentDateTimeUtc().toTime_t() instead [-Wclazy-qdatetime-utc]
+clazy-standalone/header_filter_bar.h:5:5: warning: Use QDateTime::currentDateTimeUtc().toTime_t() instead [-Wclazy-qdatetime-utc]
diff --git a/tests/clazy-standalone/fileToNotIgnore.cpp b/tests/clazy-standalone/fileToNotIgnore.cpp
new file mode 100644
index 00000000..33eebaac
--- /dev/null
+++ b/tests/clazy-standalone/fileToNotIgnore.cpp
@@ -0,0 +1,6 @@
+#include <QtCore/QDateTime>
+
+void t()
+{
+ QDateTime::currentDateTime().toTime_t();
+}
diff --git a/tests/clazy-standalone/fileToNotIgnore.cpp.expected b/tests/clazy-standalone/fileToNotIgnore.cpp.expected
new file mode 100644
index 00000000..6f14967d
--- /dev/null
+++ b/tests/clazy-standalone/fileToNotIgnore.cpp.expected
@@ -0,0 +1 @@
+clazy-standalone/fileToNotIgnore.cpp:5:5: warning: Use QDateTime::currentDateTimeUtc().toTime_t() instead [-Wclazy-qdatetime-utc]
diff --git a/tests/run_tests.py b/tests/run_tests.py
index a285e401..65a65c43 100755
--- a/tests/run_tests.py
+++ b/tests/run_tests.py
@@ -37,6 +37,7 @@ class Test:
self.only_qt = False
self.qt_developer = False
self.header_filter = ""
+ self.ignore_dirs = ""
def isScript(self):
return self.filename.endswith(".sh")
@@ -159,7 +160,8 @@ def load_json(check_name):
test.qt_developer = t['qt_developer']
if 'header_filter' in t:
test.header_filter = t['header_filter']
-
+ if 'ignore_dirs' in t:
+ test.ignore_dirs = t['ignore_dirs']
if not test.checks:
test.checks.append(test.check.name)
@@ -233,6 +235,9 @@ def clazy_standalone_command(test, qt):
if test.header_filter:
result = " -header-filter " + test.header_filter + " " + result
+ if test.ignore_dirs:
+ result = " -ignore-dirs " + test.ignore_dirs + " " + result
+
return result
def clazy_command(qt, test, filename):