| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileContributor: Sérgio Martins <[email protected]> |
| 4 |
|
| 5 |
SPDX-FileCopyrightText: 2015-2017 Sergio Martins <[email protected]> |
| 6 |
|
| 7 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef CHECK_BASE_H |
| 11 |
#define CHECK_BASE_H |
| 12 |
|
| 13 |
#include "ClazyContext.h" |
| 14 |
#include "clazy_stl.h" // IWYU pragma: keep |
| 15 |
|
| 16 |
#include <clang/AST/ASTContext.h> |
| 17 |
#include <clang/ASTMatchers/ASTMatchFinder.h> |
| 18 |
#include <clang/ASTMatchers/ASTMatchers.h> |
| 19 |
#include <clang/Basic/SourceLocation.h> |
| 20 |
#include <clang/Basic/SourceManager.h> |
| 21 |
#include <clang/Frontend/CompilerInstance.h> |
| 22 |
#include <clang/Lex/PPCallbacks.h> |
| 23 |
#include <clang/Parse/Parser.h> |
| 24 |
#include <llvm/Config/llvm-config.h> |
| 25 |
|
| 26 |
#include <string> |
| 27 |
#include <utility> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
namespace clazy |
| 31 |
{ |
| 32 |
using OptionalFileEntryRef = clang::CustomizableOptional<clang::FileEntryRef>; |
| 33 |
} |
| 34 |
|
| 35 |
namespace clang |
| 36 |
{ |
| 37 |
class CXXMethodDecl; |
| 38 |
class Stmt; |
| 39 |
class Decl; |
| 40 |
class TranslationUnitDecl; |
| 41 |
class FixItHint; |
| 42 |
class PresumedLoc; |
| 43 |
class SourceLocation; |
| 44 |
class PreprocessorOptions; |
| 45 |
class LangOptions; |
| 46 |
class MacroArgs; |
| 47 |
class MacroDefinition; |
| 48 |
class MacroDirective; |
| 49 |
class MacroInfo; |
| 50 |
class SourceManager; |
| 51 |
class Token; |
| 52 |
} |
| 53 |
|
| 54 |
class CheckBase; |
| 55 |
|
| 56 |
enum CheckLevel { // See README.md for what each level does |
| 57 |
CheckLevelUndefined = -1, |
| 58 |
CheckLevel0 = 0, |
| 59 |
CheckLevel1, |
| 60 |
CheckLevel2, |
| 61 |
ManualCheckLevel, |
| 62 |
MaxCheckLevel = CheckLevel2, |
| 63 |
DefaultCheckLevel = CheckLevel1 |
| 64 |
}; |
| 65 |
|
| 66 |
class ClazyPreprocessorCallbacks : public clang::PPCallbacks |
| 67 |
{ |
| 68 |
public: |
| 69 |
ClazyPreprocessorCallbacks(const ClazyPreprocessorCallbacks &) = delete; |
| 70 |
explicit ClazyPreprocessorCallbacks(CheckBase *check); |
| 71 |
|
| 72 |
void MacroExpands(const clang::Token &MacroNameTok, const clang::MacroDefinition &, clang::SourceRange, const clang::MacroArgs *) override; |
| 73 |
void MacroDefined(const clang::Token &MacroNameTok, const clang::MacroDirective *) override; |
| 74 |
void Defined(const clang::Token &MacroNameTok, const clang::MacroDefinition &, clang::SourceRange Range) override; |
| 75 |
void Ifdef(clang::SourceLocation, const clang::Token &MacroNameTok, const clang::MacroDefinition &) override; |
| 76 |
void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok, const clang::MacroDefinition &) override; |
| 77 |
void If(clang::SourceLocation loc, clang::SourceRange conditionRange, clang::PPCallbacks::ConditionValueKind conditionValue) override; |
| 78 |
void Elif(clang::SourceLocation loc, |
| 79 |
clang::SourceRange conditionRange, |
| 80 |
clang::PPCallbacks::ConditionValueKind ConditionValue, |
| 81 |
clang::SourceLocation ifLoc) override; |
| 82 |
void Else(clang::SourceLocation loc, clang::SourceLocation ifLoc) override; |
| 83 |
void Endif(clang::SourceLocation loc, clang::SourceLocation ifLoc) override; |
| 84 |
void InclusionDirective(clang::SourceLocation HashLoc, |
| 85 |
const clang::Token &IncludeTok, |
| 86 |
llvm::StringRef FileName, |
| 87 |
bool IsAngled, |
| 88 |
clang::CharSourceRange FilenameRange, |
| 89 |
clazy::OptionalFileEntryRef File, |
| 90 |
llvm::StringRef SearchPath, |
| 91 |
llvm::StringRef RelativePath, |
| 92 |
const clang::Module *SuggestedModule, |
| 93 |
bool ModuleImported, |
| 94 |
clang::SrcMgr::CharacteristicKind FileType) override; |
| 95 |
|
| 96 |
private: |
| 97 |
CheckBase *const check; |
| 98 |
}; |
| 99 |
|
| 100 |
class ClazyAstMatcherCallback : public clang::ast_matchers::MatchFinder::MatchCallback |
| 101 |
{ |
| 102 |
public: |
| 103 |
explicit ClazyAstMatcherCallback(CheckBase *check); |
| 104 |
|
| 105 |
protected: |
| 106 |
CheckBase *const m_check; |
| 107 |
}; |
| 108 |
|
| 109 |
class CheckBase |
| 110 |
{ |
| 111 |
public: |
| 112 |
enum Option { Option_None = 0, Option_CanIgnoreIncludes = 1 }; |
| 113 |
using Options = int; |
| 114 |
|
| 115 |
using List = std::vector<CheckBase *>; |
| 116 |
explicit CheckBase(const std::string &name, Options = Option_None); |
| 117 |
CheckBase(const CheckBase &other) = delete; |
| 118 |
|
| 119 |
virtual ~CheckBase(); |
| 120 |
|
| 121 |
std::string name() const |
| 122 |
{ |
| 123 |
return m_name; |
| 124 |
} |
| 125 |
|
| 126 |
void emitWarning(const clang::Decl *, const std::string &error, bool printWarningTag = true); |
| 127 |
void emitWarning(const clang::Stmt *, const std::string &error, bool printWarningTag = true); |
| 128 |
void emitWarning(clang::SourceLocation loc, const std::string &error, bool printWarningTag = true); |
| 129 |
void emitWarning(clang::SourceLocation loc, std::string error, const std::vector<clang::FixItHint> &fixits, bool printWarningTag = true); |
| 130 |
void emitInternalError(clang::SourceLocation loc, std::string error); |
| 131 |
|
| 132 |
virtual void registerASTMatchers(clang::ast_matchers::MatchFinder &) |
| 133 |
{ |
| 134 |
} |
| 135 |
|
| 136 |
bool canIgnoreIncludes() const |
| 137 |
{ |
| 138 |
return m_options & Option_CanIgnoreIncludes; |
| 139 |
} |
| 140 |
|
| 141 |
virtual void VisitStmt(clang::Stmt *stm); |
| 142 |
virtual void VisitDecl(clang::Decl *decl); |
| 143 |
void enablePreProcessorCallbacks(clang::Preprocessor &pp); |
| 144 |
|
| 145 |
// Lazy initialized for clang-tidy plugin. Value will be assigned before Visit* methods are called |
| 146 |
const ClazyContext *m_context = nullptr; |
| 147 |
|
| 148 |
protected: |
| 149 |
virtual void VisitMacroExpands(const clang::Token ¯oNameTok, const clang::SourceRange &, const clang::MacroInfo *minfo = nullptr); |
| 150 |
virtual void VisitMacroDefined(const clang::Token ¯oNameTok); |
| 151 |
virtual void VisitDefined(const clang::Token ¯oNameTok, const clang::SourceRange &); |
| 152 |
virtual void VisitIfdef(clang::SourceLocation, const clang::Token &); |
| 153 |
virtual void VisitIfndef(clang::SourceLocation, const clang::Token &); |
| 154 |
virtual void VisitIf(clang::SourceLocation loc, clang::SourceRange conditionRange, clang::PPCallbacks::ConditionValueKind conditionValue); |
| 155 |
virtual void |
| 156 |
VisitElif(clang::SourceLocation loc, clang::SourceRange conditionRange, clang::PPCallbacks::ConditionValueKind ConditionValue, clang::SourceLocation ifLoc); |
| 157 |
virtual void VisitElse(clang::SourceLocation loc, clang::SourceLocation ifLoc); |
| 158 |
virtual void VisitEndif(clang::SourceLocation loc, clang::SourceLocation ifLoc); |
| 159 |
virtual void VisitInclusionDirective(clang::SourceLocation HashLoc, |
| 160 |
const clang::Token &IncludeTok, |
| 161 |
llvm::StringRef FileName, |
| 162 |
bool IsAngled, |
| 163 |
clang::CharSourceRange FilenameRange, |
| 164 |
clazy::OptionalFileEntryRef File, |
| 165 |
llvm::StringRef SearchPath, |
| 166 |
llvm::StringRef RelativePath, |
| 167 |
const clang::Module *SuggestedModule, |
| 168 |
bool ModuleImported, |
| 169 |
clang::SrcMgr::CharacteristicKind FileType); |
| 170 |
|
| 171 |
bool shouldIgnoreFile(clang::SourceLocation) const; |
| 172 |
void reallyEmitWarning(clang::SourceLocation loc, const std::string &error, const std::vector<clang::FixItHint> &fixits); |
| 173 |
|
| 174 |
void queueManualFixitWarning(clang::SourceLocation loc, const std::string &message = {}); |
| 175 |
bool warningAlreadyEmitted(clang::SourceLocation loc) const; |
| 176 |
bool manualFixitAlreadyQueued(clang::SourceLocation loc) const; |
| 177 |
bool isOptionSet(const std::string &optionName) const; |
| 178 |
|
| 179 |
// 3 shortcuts for stuff that litter the codebase all over. |
| 180 |
const clang::SourceManager &sm() const |
| 181 |
{ |
| 182 |
return m_context->sm; |
| 183 |
} |
| 184 |
const clang::LangOptions &lo() const |
| 185 |
{ |
| 186 |
return m_context->lo; |
| 187 |
} |
| 188 |
|
| 189 |
clang::ASTContext *astContext() const |
| 190 |
{ |
| 191 |
return m_context->astContext; |
| 192 |
} |
| 193 |
|
| 194 |
std::string qtNamespaced(const std::string &className) const |
| 195 |
{ |
| 196 |
const std::string qtNsp = m_context->qtNamespace(); |
| 197 |
return qtNsp.empty() ? className : qtNsp + "::" + className; |
| 198 |
} |
| 199 |
|
| 200 |
std::string trimQtNamespace(std::string &&name) const |
| 201 |
{ |
| 202 |
if (!m_context->qtNamespace().empty() && name.starts_with(m_context->qtNamespace() + "::")) { |
| 203 |
name.erase(0, m_context->qtNamespace().size() + 2); |
| 204 |
} |
| 205 |
return name; |
| 206 |
} |
| 207 |
|
| 208 |
const std::string m_name; |
| 209 |
std::vector<std::string> m_filesToIgnore; |
| 210 |
|
| 211 |
private: |
| 212 |
std::string tag() const; |
| 213 |
friend class ClazyPreprocessorCallbacks; |
| 214 |
friend class ClazyAstMatcherCallback; |
| 215 |
ClazyPreprocessorCallbacks *const m_preprocessorCallbacks; |
| 216 |
std::vector<unsigned int> m_emittedWarningsInMacro; |
| 217 |
std::vector<unsigned int> m_emittedManualFixItsWarningsInMacro; |
| 218 |
std::vector<std::pair<clang::SourceLocation, std::string>> m_queuedManualInterventionWarnings; |
| 219 |
const Options m_options; |
| 220 |
}; |
| 221 |
|
| 222 |
#endif |