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 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 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 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 Finder->addMatcher(typeLoc(loc(isInteger())).bind("tl"), this); 61 IdentTable = llvm::make_unique<IdentifierTable>(getLangOpts()); 62 } 63 64 void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) { 65 auto TL = *Result.Nodes.getNodeAs<TypeLoc>("tl"); 66 SourceLocation Loc = TL.getLocStart(); 67 68 if (Loc.isInvalid() || Loc.isMacroID()) 69 return; 70 71 // Look through qualification. 72 if (auto QualLoc = TL.getAs<QualifiedTypeLoc>()) 73 TL = QualLoc.getUnqualifiedLoc(); 74 75 auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>(); 76 if (!BuiltinLoc) 77 return; 78 79 Token Tok = getTokenAtLoc(Loc, Result, *IdentTable); 80 // Ensure the location actually points to one of the builting integral type 81 // names we're interested in. Otherwise, we might be getting this match from 82 // implicit code (e.g. an implicit assignment operator of a class containing 83 // an array of non-POD types). 84 if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned, 85 tok::kw_signed)) 86 return; 87 88 bool IsSigned; 89 unsigned Width; 90 const TargetInfo &TargetInfo = Result.Context->getTargetInfo(); 91 92 // Look for uses of short, long, long long and their unsigned versions. 93 switch (BuiltinLoc.getTypePtr()->getKind()) { 94 case BuiltinType::Short: 95 Width = TargetInfo.getShortWidth(); 96 IsSigned = true; 97 break; 98 case BuiltinType::Long: 99 Width = TargetInfo.getLongWidth(); 100 IsSigned = true; 101 break; 102 case BuiltinType::LongLong: 103 Width = TargetInfo.getLongLongWidth(); 104 IsSigned = true; 105 break; 106 case BuiltinType::UShort: 107 Width = TargetInfo.getShortWidth(); 108 IsSigned = false; 109 break; 110 case BuiltinType::ULong: 111 Width = TargetInfo.getLongWidth(); 112 IsSigned = false; 113 break; 114 case BuiltinType::ULongLong: 115 Width = TargetInfo.getLongLongWidth(); 116 IsSigned = false; 117 break; 118 default: 119 return; 120 } 121 122 // We allow "unsigned short port" as that's reasonably common and required by 123 // the sockets API. 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()])) 128 return; 129 130 std::string Replacement = 131 ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) + 132 TypeSuffix) 133 .str(); 134 135 // We don't add a fix-it as changing the type can easily break code, 136 // e.g. when a function requires a 'long' argument on all platforms. 137 // QualTypes are printed with implicit quotes. 138 diag(Loc, "consider replacing %0 with '%1'") << BuiltinLoc.getType() 139 << Replacement; 140 } 141 142 } // namespace runtime 143 } // namespace google 144 } // namespace tidy 145 } // namespace clang 146