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());
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);
60 Finder->addMatcher(typeLoc(loc(isInteger())).bind(
"tl"),
this);
61 IdentTable = llvm::make_unique<IdentifierTable>(
getLangOpts());
65 auto TL = *Result.Nodes.getNodeAs<TypeLoc>(
"tl");
66 SourceLocation
Loc = TL.getLocStart();
68 if (Loc.isInvalid() || Loc.isMacroID())
72 if (
auto QualLoc = TL.getAs<QualifiedTypeLoc>())
73 TL = QualLoc.getUnqualifiedLoc();
75 auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
84 if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned,
90 const TargetInfo &TargetInfo = Result.Context->getTargetInfo();
93 switch (BuiltinLoc.getTypePtr()->getKind()) {
94 case BuiltinType::Short:
95 Width = TargetInfo.getShortWidth();
98 case BuiltinType::Long:
99 Width = TargetInfo.getLongWidth();
102 case BuiltinType::LongLong:
103 Width = TargetInfo.getLongLongWidth();
106 case BuiltinType::UShort:
107 Width = TargetInfo.getShortWidth();
110 case BuiltinType::ULong:
111 Width = TargetInfo.getLongWidth();
114 case BuiltinType::ULongLong:
115 Width = TargetInfo.getLongLongWidth();
124 const StringRef Port =
"unsigned short port";
125 const char *Data = Result.SourceManager->getCharacterData(Loc);
126 if (!std::strncmp(Data, Port.data(), Port.size()) &&
127 !isIdentifierBody(Data[Port.size()]))
130 std::string Replacement =
131 ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
138 diag(Loc,
"consider replacing %0 with '%1'") << BuiltinLoc.getType()
SourceLocation Loc
'#' location in the include directive
LangOptions getLangOpts() const
Returns the language options from the context.
std::unique_ptr< ast_matchers::MatchFinder > Finder
Base class for all clang-tidy checks.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
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.
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
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.