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/TargetInfo.h" 16 17 namespace clang { 18 namespace tidy { 19 namespace runtime { 20 21 using namespace ast_matchers; 22 23 void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) { 24 // Find all TypeLocs. 25 Finder->addMatcher(typeLoc().bind("tl"), this); 26 } 27 28 void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) { 29 // The relevant Style Guide rule only applies to C++. 30 if (!Result.Context->getLangOpts().CPlusPlus) 31 return; 32 33 auto TL = *Result.Nodes.getNodeAs<TypeLoc>("tl"); 34 SourceLocation Loc = TL.getLocStart(); 35 36 if (Loc.isInvalid() || Loc.isMacroID()) 37 return; 38 39 // Look through qualification. 40 if (auto QualLoc = TL.getAs<QualifiedTypeLoc>()) 41 TL = QualLoc.getUnqualifiedLoc(); 42 43 auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>(); 44 if (!BuiltinLoc) 45 return; 46 47 bool IsSigned; 48 unsigned Width; 49 const TargetInfo &TargetInfo = Result.Context->getTargetInfo(); 50 51 // Look for uses of short, long, long long and their unsigned versions. 52 switch (BuiltinLoc.getTypePtr()->getKind()) { 53 case BuiltinType::Short: 54 Width = TargetInfo.getShortWidth(); 55 IsSigned = true; 56 break; 57 case BuiltinType::Long: 58 Width = TargetInfo.getLongWidth(); 59 IsSigned = true; 60 break; 61 case BuiltinType::LongLong: 62 Width = TargetInfo.getLongLongWidth(); 63 IsSigned = true; 64 break; 65 case BuiltinType::UShort: 66 Width = TargetInfo.getShortWidth(); 67 IsSigned = false; 68 break; 69 case BuiltinType::ULong: 70 Width = TargetInfo.getLongWidth(); 71 IsSigned = false; 72 break; 73 case BuiltinType::ULongLong: 74 Width = TargetInfo.getLongLongWidth(); 75 IsSigned = false; 76 break; 77 default: 78 return; 79 } 80 81 // We allow "unsigned short port" as that's reasonably common and required by 82 // the sockets API. 83 const StringRef Port = "unsigned short port"; 84 const char *Data = Result.SourceManager->getCharacterData(Loc); 85 if (!std::strncmp(Data, Port.data(), Port.size()) && 86 !isIdentifierBody(Data[Port.size()])) 87 return; 88 89 std::string Replacement = 90 ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) + 91 (AddUnderscoreT ? "_t" : "")).str(); 92 93 // We don't add a fix-it as changing the type can easily break code, 94 // e.g. when a function requires a 'long' argument on all platforms. 95 // QualTypes are printed with implicit quotes. 96 diag(Loc, "consider replacing %0 with '%1'") << BuiltinLoc.getType() 97 << Replacement; 98 } 99 100 } // namespace runtime 101 } // namespace tidy 102 } // namespace clang 103