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