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"
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);
61 Finder->addMatcher(typeLoc(loc(isInteger()),
62 unless(hasAncestor(callExpr(
63 callee(functionDecl(hasAttr(attr::Format)))))))
66 IdentTable = std::make_unique<IdentifierTable>(
getLangOpts());
70 auto TL = *Result.Nodes.getNodeAs<TypeLoc>(
"tl");
71 SourceLocation
Loc = TL.getBeginLoc();
73 if (
Loc.isInvalid() ||
Loc.isMacroID())
77 if (
auto QualLoc = TL.getAs<QualifiedTypeLoc>())
78 TL = QualLoc.getUnqualifiedLoc();
80 auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
89 if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned,
95 const TargetInfo &TargetInfo = Result.Context->getTargetInfo();
98 switch (BuiltinLoc.getTypePtr()->getKind()) {
99 case BuiltinType::Short:
100 Width = TargetInfo.getShortWidth();
103 case BuiltinType::Long:
104 Width = TargetInfo.getLongWidth();
107 case BuiltinType::LongLong:
108 Width = TargetInfo.getLongLongWidth();
111 case BuiltinType::UShort:
112 Width = TargetInfo.getShortWidth();
115 case BuiltinType::ULong:
116 Width = TargetInfo.getLongWidth();
119 case BuiltinType::ULongLong:
120 Width = TargetInfo.getLongLongWidth();
129 const StringRef Port =
"unsigned short port";
130 const char *Data = Result.SourceManager->getCharacterData(
Loc);
131 if (!std::strncmp(Data, Port.data(), Port.size()) &&
132 !isIdentifierBody(Data[Port.size()]))
135 std::string Replacement =
136 ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
143 diag(
Loc,
"consider replacing %0 with '%1'") << BuiltinLoc.getType()