diff options
Diffstat (limited to 'src/ClazyContext.h')
-rw-r--r-- | src/ClazyContext.h | 68 |
1 files changed, 65 insertions, 3 deletions
diff --git a/src/ClazyContext.h b/src/ClazyContext.h index 758f9c0a..608e86b4 100644 --- a/src/ClazyContext.h +++ b/src/ClazyContext.h @@ -28,9 +28,12 @@ #include <clang/Frontend/CompilerInstance.h> #include <clang/Lex/PreprocessorOptions.h> +#include <clang/Basic/FileManager.h> +#include <llvm/Support/Regex.h> #include <string> #include <vector> +#include <memory> // ClazyContext is just a struct to share data and code between all checks @@ -57,11 +60,15 @@ public: ClazyOption_Qt4Compat = 8, ClazyOption_OnlyQt = 16, // Ignore non-Qt files. This is done by bailing out if QT_CORE_LIB is not set. ClazyOption_QtDeveloper = 32, // For running clazy on Qt itself, optional, but honours specific guidelines - ClazyOption_VisitImplicitCode = 64 // Inspect compiler generated code aswell, useful for custom checks, if then need it + ClazyOption_VisitImplicitCode = 64, // Inspect compiler generated code aswell, useful for custom checks, if they need it + ClazyOption_IgnoreIncludedFiles = 128 // Only warn for the current file being compiled, not on includes (useful for performance reasons) }; typedef int ClazyOptions; - explicit ClazyContext(const clang::CompilerInstance &ci, ClazyOptions = ClazyOption_None); + explicit ClazyContext(const clang::CompilerInstance &ci, + const std::string &headerFilter, + const std::string &ignoreDirs, + ClazyOptions = ClazyOption_None); ~ClazyContext(); bool usingPreCompiledHeaders() const @@ -89,6 +96,11 @@ public: return options & ClazyOption_QtDeveloper; } + bool ignoresIncludedFiles() const + { + return options & ClazyOption_IgnoreIncludedFiles; + } + bool isVisitImplicitCode() const { return options & ClazyContext::ClazyOption_VisitImplicitCode; @@ -96,7 +108,53 @@ public: bool isOptionSet(const std::string &optionName) const { - return clazy_std::contains(extraOptions, optionName); + return clazy::contains(extraOptions, optionName); + } + + bool fileMatchesLoc(const std::unique_ptr<llvm::Regex> ®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; + + const bool matches = fileMatchesLoc(headerFilterRegex, loc, &file); + if (!file) + return false; + + return !matches; + } + + bool isMainFile(clang::SourceLocation loc) const + { + if (loc.isMacroID()) + loc = sm.getExpansionLoc(loc); + + return sm.isInFileID(loc, sm.getMainFileID()); } /** @@ -120,6 +178,10 @@ public: clang::FixItRewriter *rewriter = nullptr; bool allFixitsEnabled = false; std::string requestedFixitName; + clang::CXXMethodDecl *lastMethodDecl = nullptr; + clang::Decl *lastDecl = nullptr; + std::unique_ptr<llvm::Regex> headerFilterRegex; + std::unique_ptr<llvm::Regex> ignoreDirsRegex; }; #endif |