11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 14 #include "clang/Basic/CharInfo.h" 15 #include "clang/Basic/IdentifierTable.h" 16 #include "clang/Basic/TargetInfo.h" 17 #include "clang/Lex/Lexer.h" 21 using namespace ast_matchers;
24 const MatchFinder::MatchResult &MatchResult,
25 IdentifierTable &IdentTable) {
27 if (Lexer::getRawToken(Loc, Tok, *MatchResult.SourceManager,
28 MatchResult.Context->getLangOpts(),
false))
31 if (Tok.is(tok::raw_identifier)) {
32 IdentifierInfo &
Info = IdentTable.get(Tok.getRawIdentifier());
33 Tok.setIdentifierInfo(&Info);
34 Tok.setKind(Info.getTokenID());
45 UnsignedTypePrefix(Options.get(
"UnsignedTypePrefix",
"uint")),
46 SignedTypePrefix(Options.get(
"SignedTypePrefix",
"int")),
47 TypeSuffix(Options.get(
"TypeSuffix",
"")) {}
50 Options.
store(Opts,
"UnsignedTypePrefix", UnsignedTypePrefix);
51 Options.
store(Opts,
"SignedTypePrefix", SignedTypePrefix);
59 Finder->addMatcher(typeLoc(loc(isInteger())).bind(
"tl"),
this);
60 IdentTable = llvm::make_unique<IdentifierTable>(
getLangOpts());
64 auto TL = *Result.Nodes.getNodeAs<TypeLoc>(
"tl");
65 SourceLocation
Loc = TL.getLocStart();
67 if (Loc.isInvalid() || Loc.isMacroID())
71 if (
auto QualLoc = TL.getAs<QualifiedTypeLoc>())
72 TL = QualLoc.getUnqualifiedLoc();
74 auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
83 if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned,
89 const TargetInfo &TargetInfo = Result.Context->getTargetInfo();
92 switch (BuiltinLoc.getTypePtr()->getKind()) {
93 case BuiltinType::Short:
94 Width = TargetInfo.getShortWidth();
97 case BuiltinType::Long:
98 Width = TargetInfo.getLongWidth();
101 case BuiltinType::LongLong:
102 Width = TargetInfo.getLongLongWidth();
105 case BuiltinType::UShort:
106 Width = TargetInfo.getShortWidth();
109 case BuiltinType::ULong:
110 Width = TargetInfo.getLongWidth();
113 case BuiltinType::ULongLong:
114 Width = TargetInfo.getLongLongWidth();
123 const StringRef Port =
"unsigned short port";
124 const char *Data = Result.SourceManager->getCharacterData(Loc);
125 if (!std::strncmp(Data, Port.data(), Port.size()) &&
126 !isIdentifierBody(Data[Port.size()]))
129 std::string Replacement =
130 ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
137 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.
std::map< std::string, std::string > OptionMap
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.