1 //===--- IntegerTypesCheck.cpp - clang-tidy -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "IntegerTypesCheck.h"
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"
19 
20 namespace clang {
21 
22 using namespace ast_matchers;
23 
getTokenAtLoc(SourceLocation Loc,const MatchFinder::MatchResult & MatchResult,IdentifierTable & IdentTable)24 static Token getTokenAtLoc(SourceLocation Loc,
25                            const MatchFinder::MatchResult &MatchResult,
26                            IdentifierTable &IdentTable) {
27   Token Tok;
28   if (Lexer::getRawToken(Loc, Tok, *MatchResult.SourceManager,
29                          MatchResult.Context->getLangOpts(), false))
30     return Tok;
31 
32   if (Tok.is(tok::raw_identifier)) {
33     IdentifierInfo &Info = IdentTable.get(Tok.getRawIdentifier());
34     Tok.setIdentifierInfo(&Info);
35     Tok.setKind(Info.getTokenID());
36   }
37   return Tok;
38 }
39 
40 namespace tidy {
41 namespace google {
42 namespace runtime {
43 
IntegerTypesCheck(StringRef Name,ClangTidyContext * Context)44 IntegerTypesCheck::IntegerTypesCheck(StringRef Name, ClangTidyContext *Context)
45     : ClangTidyCheck(Name, Context),
46       UnsignedTypePrefix(Options.get("UnsignedTypePrefix", "uint")),
47       SignedTypePrefix(Options.get("SignedTypePrefix", "int")),
48       TypeSuffix(Options.get("TypeSuffix", "")) {}
49 
storeOptions(ClangTidyOptions::OptionMap & Opts)50 void IntegerTypesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
51   Options.store(Opts, "UnsignedTypePrefix", UnsignedTypePrefix);
52   Options.store(Opts, "SignedTypePrefix", SignedTypePrefix);
53   Options.store(Opts, "TypeSuffix", TypeSuffix);
54 }
55 
registerMatchers(MatchFinder * Finder)56 void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
57   // Find all TypeLocs. The relevant Style Guide rule only applies to C++.
58   if (!getLangOpts().CPlusPlus)
59     return;
60   // Match any integer types, unless they are passed to a printf-based API:
61   //
62   // http://google.github.io/styleguide/cppguide.html#64-bit_Portability
63   // "Where possible, avoid passing arguments of types specified by
64   // bitwidth typedefs to printf-based APIs."
65   Finder->addMatcher(typeLoc(loc(isInteger()),
66                              unless(hasAncestor(callExpr(
67                                  callee(functionDecl(hasAttr(attr::Format)))))))
68                          .bind("tl"),
69                      this);
70   IdentTable = llvm::make_unique<IdentifierTable>(getLangOpts());
71 }
72 
check(const MatchFinder::MatchResult & Result)73 void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
74   auto TL = *Result.Nodes.getNodeAs<TypeLoc>("tl");
75   SourceLocation Loc = TL.getLocStart();
76 
77   if (Loc.isInvalid() || Loc.isMacroID())
78     return;
79 
80   // Look through qualification.
81   if (auto QualLoc = TL.getAs<QualifiedTypeLoc>())
82     TL = QualLoc.getUnqualifiedLoc();
83 
84   auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
85   if (!BuiltinLoc)
86     return;
87 
88   Token Tok = getTokenAtLoc(Loc, Result, *IdentTable);
89   // Ensure the location actually points to one of the builting integral type
90   // names we're interested in. Otherwise, we might be getting this match from
91   // implicit code (e.g. an implicit assignment operator of a class containing
92   // an array of non-POD types).
93   if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned,
94                    tok::kw_signed))
95     return;
96 
97   bool IsSigned;
98   unsigned Width;
99   const TargetInfo &TargetInfo = Result.Context->getTargetInfo();
100 
101   // Look for uses of short, long, long long and their unsigned versions.
102   switch (BuiltinLoc.getTypePtr()->getKind()) {
103   case BuiltinType::Short:
104     Width = TargetInfo.getShortWidth();
105     IsSigned = true;
106     break;
107   case BuiltinType::Long:
108     Width = TargetInfo.getLongWidth();
109     IsSigned = true;
110     break;
111   case BuiltinType::LongLong:
112     Width = TargetInfo.getLongLongWidth();
113     IsSigned = true;
114     break;
115   case BuiltinType::UShort:
116     Width = TargetInfo.getShortWidth();
117     IsSigned = false;
118     break;
119   case BuiltinType::ULong:
120     Width = TargetInfo.getLongWidth();
121     IsSigned = false;
122     break;
123   case BuiltinType::ULongLong:
124     Width = TargetInfo.getLongLongWidth();
125     IsSigned = false;
126     break;
127   default:
128     return;
129   }
130 
131   // We allow "unsigned short port" as that's reasonably common and required by
132   // the sockets API.
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()]))
137     return;
138 
139   std::string Replacement =
140       ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
141        TypeSuffix)
142           .str();
143 
144   // We don't add a fix-it as changing the type can easily break code,
145   // e.g. when a function requires a 'long' argument on all platforms.
146   // QualTypes are printed with implicit quotes.
147   diag(Loc, "consider replacing %0 with '%1'") << BuiltinLoc.getType()
148                                                << Replacement;
149 }
150 
151 } // namespace runtime
152 } // namespace google
153 } // namespace tidy
154 } // namespace clang
155