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