10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Basic/AttrKinds.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"
21using 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());
41 return Node.getLiteralIdentifier() !=
nullptr;
45 const SourceLocation
Loc =
Node.getBeginLoc();
46 return Loc.isValid() && !
Loc.isMacroID();
51 if (
auto QualLoc =
Node.getAs<QualifiedTypeLoc>())
52 TL = QualLoc.getUnqualifiedLoc();
54 const auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
58 switch (BuiltinLoc.getTypePtr()->getKind()) {
59 case BuiltinType::Short:
60 case BuiltinType::Long:
61 case BuiltinType::LongLong:
62 case BuiltinType::UShort:
63 case BuiltinType::ULong:
64 case BuiltinType::ULongLong:
73namespace tidy::google::runtime {
77 UnsignedTypePrefix(Options.get(
"UnsignedTypePrefix",
"uint")),
78 SignedTypePrefix(Options.get(
"SignedTypePrefix",
"int")),
79 TypeSuffix(Options.get(
"TypeSuffix",
"")) {}
82 Options.
store(Opts,
"UnsignedTypePrefix", UnsignedTypePrefix);
83 Options.
store(Opts,
"SignedTypePrefix", SignedTypePrefix);
94 typeLoc(loc(isInteger()), isValidAndNotInMacro(), isBuiltinType(),
96 callExpr(callee(functionDecl(hasAttr(attr::Format)))))),
97 unless(hasParent(parmVarDecl(
98 hasAncestor(functionDecl(isUserDefineLiteral()))))))
101 IdentTable = std::make_unique<IdentifierTable>(
getLangOpts());
105 auto TL = *Result.Nodes.getNodeAs<TypeLoc>(
"tl");
106 SourceLocation
Loc = TL.getBeginLoc();
109 if (
auto QualLoc = TL.getAs<QualifiedTypeLoc>())
110 TL = QualLoc.getUnqualifiedLoc();
112 auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
121 if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned,
125 bool IsSigned =
false;
127 const TargetInfo &TargetInfo = Result.Context->getTargetInfo();
130 switch (BuiltinLoc.getTypePtr()->getKind()) {
131 case BuiltinType::Short:
132 Width = TargetInfo.getShortWidth();
135 case BuiltinType::Long:
136 Width = TargetInfo.getLongWidth();
139 case BuiltinType::LongLong:
140 Width = TargetInfo.getLongLongWidth();
143 case BuiltinType::UShort:
144 Width = TargetInfo.getShortWidth();
147 case BuiltinType::ULong:
148 Width = TargetInfo.getLongWidth();
151 case BuiltinType::ULongLong:
152 Width = TargetInfo.getLongLongWidth();
161 const StringRef Port =
"unsigned short port";
162 const char *Data = Result.SourceManager->getCharacterData(
Loc);
163 if (!std::strncmp(Data, Port.data(), Port.size()) &&
164 !isAsciiIdentifierContinue(Data[Port.size()]))
167 std::string Replacement =
168 ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
175 diag(
Loc,
"consider replacing %0 with '%1'") << BuiltinLoc.getType()
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
IntegerTypesCheck(StringRef Name, ClangTidyContext *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 &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static Token getTokenAtLoc(SourceLocation Loc, const MatchFinder::MatchResult &MatchResult, IdentifierTable &IdentTable)
llvm::StringMap< ClangTidyValue > OptionMap