clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

IntegerTypesCheck.cpp
Go to the documentation of this file.
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 google {
20 namespace runtime {
21
22 using namespace ast_matchers;
23
24 IntegerTypesCheck::IntegerTypesCheck(StringRef Name, ClangTidyContext *Context)
25 : ClangTidyCheck(Name, Context),
26 UnsignedTypePrefix(Options.get("UnsignedTypePrefix", "uint")),
27 SignedTypePrefix(Options.get("SignedTypePrefix", "int")),
28 TypeSuffix(Options.get("TypeSuffix", "")) {}
29
30 void IntegerTypesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
31 Options.store(Opts, "UnsignedTypePrefix", UnsignedTypePrefix);
32 Options.store(Opts, "SignedTypePrefix", SignedTypePrefix);
33 Options.store(Opts, "TypeSuffix", TypeSuffix);
34 }
35
36 void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
37 // Find all TypeLocs. The relevant Style Guide rule only applies to C++.
38 if (getLangOpts().CPlusPlus)
39 Finder->addMatcher(typeLoc(loc(isInteger())).bind("tl"), this);
40 }
41
42 void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
43 auto TL = *Result.Nodes.getNodeAs<TypeLoc>("tl");
44 SourceLocation Loc = TL.getLocStart();
45
46 if (Loc.isInvalid() || Loc.isMacroID())
47 return;
48
49 // Look through qualification.
50 if (auto QualLoc = TL.getAs<QualifiedTypeLoc>())
51 TL = QualLoc.getUnqualifiedLoc();
52
53 auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
54 if (!BuiltinLoc)
55 return;
56
57 bool IsSigned;
58 unsigned Width;
59 const TargetInfo &TargetInfo = Result.Context->getTargetInfo();
60
61 // Look for uses of short, long, long long and their unsigned versions.
62 switch (BuiltinLoc.getTypePtr()->getKind()) {
63 case BuiltinType::Short:
64 Width = TargetInfo.getShortWidth();
65 IsSigned = true;
66 break;
67 case BuiltinType::Long:
68 Width = TargetInfo.getLongWidth();
69 IsSigned = true;
70 break;
71 case BuiltinType::LongLong:
72 Width = TargetInfo.getLongLongWidth();
73 IsSigned = true;
74 break;
75 case BuiltinType::UShort:
76 Width = TargetInfo.getShortWidth();
77 IsSigned = false;
78 break;
79 case BuiltinType::ULong:
80 Width = TargetInfo.getLongWidth();
81 IsSigned = false;
82 break;
83 case BuiltinType::ULongLong:
84 Width = TargetInfo.getLongLongWidth();
85 IsSigned = false;
86 break;
87 default:
88 return;
89 }
90
91 // We allow "unsigned short port" as that's reasonably common and required by
92 // the sockets API.
93 const StringRef Port = "unsigned short port";
94 const char *Data = Result.SourceManager->getCharacterData(Loc);
95 if (!std::strncmp(Data, Port.data(), Port.size()) &&
96 !isIdentifierBody(Data[Port.size()]))
97 return;
98
99 std::string Replacement =
100 ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
101 TypeSuffix)
102 .str();
103
104 // We don't add a fix-it as changing the type can easily break code,
105 // e.g. when a function requires a 'long' argument on all platforms.
106 // QualTypes are printed with implicit quotes.
107 diag(Loc, "consider replacing %0 with '%1'") << BuiltinLoc.getType()
108 << Replacement;
109 }
110
111 } // namespace runtime
112 } // namespace google
113 } // namespace tidy
114 } // namespace clang
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:34
clang::tidy::ClangTidyCheck::getLangOpts
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:162
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
clang::tidy::google::runtime::IntegerTypesCheck::IntegerTypesCheck
IntegerTypesCheck(StringRef Name, ClangTidyContext *Context)
Definition: IntegerTypesCheck.cpp:24
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:158
clang::tidy::google::runtime::IntegerTypesCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers with Finder.
Definition: IntegerTypesCheck.cpp:36
clang::tidy::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:344
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:82
IntegerTypesCheck.h
Context
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:124
clang::tidy::google::runtime::IntegerTypesCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: IntegerTypesCheck.cpp:42
clang::tidy::google::runtime::IntegerTypesCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
Definition: IntegerTypesCheck.cpp:30
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:323
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121