11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 14 #include "clang/Basic/AttrKinds.h" 15 #include "clang/Basic/CharInfo.h" 16 #include "clang/Basic/IdentifierTable.h" 17 #include "clang/Basic/TargetInfo.h" 18 #include "clang/Lex/Lexer.h" 22 using namespace ast_matchers;
25 const MatchFinder::MatchResult &MatchResult,
26 IdentifierTable &IdentTable) {
28 if (Lexer::getRawToken(Loc, Tok, *MatchResult.SourceManager,
29 MatchResult.Context->getLangOpts(),
false))
32 if (Tok.is(tok::raw_identifier)) {
33 IdentifierInfo &
Info = IdentTable.get(Tok.getRawIdentifier());
34 Tok.setIdentifierInfo(&Info);
35 Tok.setKind(Info.getTokenID());
46 UnsignedTypePrefix(Options.get(
"UnsignedTypePrefix",
"uint")),
47 SignedTypePrefix(Options.get(
"SignedTypePrefix",
"int")),
48 TypeSuffix(Options.get(
"TypeSuffix",
"")) {}
51 Options.
store(Opts,
"UnsignedTypePrefix", UnsignedTypePrefix);
52 Options.
store(Opts,
"SignedTypePrefix", SignedTypePrefix);
65 Finder->addMatcher(typeLoc(loc(isInteger()),
66 unless(hasAncestor(callExpr(
67 callee(functionDecl(hasAttr(attr::Format)))))))
70 IdentTable = llvm::make_unique<IdentifierTable>(
getLangOpts());
74 auto TL = *Result.Nodes.getNodeAs<TypeLoc>(
"tl");
75 SourceLocation
Loc = TL.getBeginLoc();
77 if (Loc.isInvalid() || Loc.isMacroID())
81 if (
auto QualLoc = TL.getAs<QualifiedTypeLoc>())
82 TL = QualLoc.getUnqualifiedLoc();
84 auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
93 if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned,
99 const TargetInfo &TargetInfo = Result.Context->getTargetInfo();
102 switch (BuiltinLoc.getTypePtr()->getKind()) {
103 case BuiltinType::Short:
104 Width = TargetInfo.getShortWidth();
107 case BuiltinType::Long:
108 Width = TargetInfo.getLongWidth();
111 case BuiltinType::LongLong:
112 Width = TargetInfo.getLongLongWidth();
115 case BuiltinType::UShort:
116 Width = TargetInfo.getShortWidth();
119 case BuiltinType::ULong:
120 Width = TargetInfo.getLongWidth();
123 case BuiltinType::ULongLong:
124 Width = TargetInfo.getLongLongWidth();
133 const StringRef Port =
"unsigned short port";
134 const char *Data = Result.SourceManager->getCharacterData(Loc);
135 if (!std::strncmp(Data, Port.data(), Port.size()) &&
136 !isIdentifierBody(Data[Port.size()]))
139 std::string Replacement =
140 ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
147 diag(Loc,
"consider replacing %0 with '%1'") << BuiltinLoc.getType()
SourceLocation Loc
'#' location in the include directive
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
LangOptions getLangOpts() const
Returns the language options from the context.
Base class for all clang-tidy checks.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
static Token getTokenAtLoc(SourceLocation Loc, const MatchFinder::MatchResult &MatchResult, IdentifierTable &IdentTable)
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.