clang-tools 19.0.0git
IntegerTypesCheck.cpp
Go to the documentation of this file.
1//===--- IntegerTypesCheck.cpp - clang-tidy -------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "IntegerTypesCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Basic/AttrKinds.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
19namespace clang {
20
21using namespace ast_matchers;
22
23static 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
39namespace {
40AST_MATCHER(FunctionDecl, isUserDefineLiteral) {
41 return Node.getLiteralIdentifier() != nullptr;
42}
43} // namespace
44
45namespace tidy::google::runtime {
46
48 : ClangTidyCheck(Name, Context),
49 UnsignedTypePrefix(Options.get("UnsignedTypePrefix", "uint")),
50 SignedTypePrefix(Options.get("SignedTypePrefix", "int")),
51 TypeSuffix(Options.get("TypeSuffix", "")) {}
52
54 Options.store(Opts, "UnsignedTypePrefix", UnsignedTypePrefix);
55 Options.store(Opts, "SignedTypePrefix", SignedTypePrefix);
56 Options.store(Opts, "TypeSuffix", TypeSuffix);
57}
58
59void IntegerTypesCheck::registerMatchers(MatchFinder *Finder) {
60 // Match any integer types, unless they are passed to a printf-based API:
61 //
62 // http://google.github.io/styleguide/cppguide.html#64-bit_Portability
63 // "Where possible, avoid passing arguments of types specified by
64 // bitwidth typedefs to printf-based APIs."
65 Finder->addMatcher(
66 typeLoc(loc(isInteger()),
67 unless(anyOf(hasAncestor(callExpr(
68 callee(functionDecl(hasAttr(attr::Format))))),
69 hasParent(parmVarDecl(hasAncestor(
70 functionDecl(isUserDefineLiteral())))))))
71 .bind("tl"),
72 this);
73 IdentTable = std::make_unique<IdentifierTable>(getLangOpts());
74}
75
76void IntegerTypesCheck::check(const MatchFinder::MatchResult &Result) {
77 auto TL = *Result.Nodes.getNodeAs<TypeLoc>("tl");
78 SourceLocation Loc = TL.getBeginLoc();
79
80 if (Loc.isInvalid() || Loc.isMacroID())
81 return;
82
83 // Look through qualification.
84 if (auto QualLoc = TL.getAs<QualifiedTypeLoc>())
85 TL = QualLoc.getUnqualifiedLoc();
86
87 auto BuiltinLoc = TL.getAs<BuiltinTypeLoc>();
88 if (!BuiltinLoc)
89 return;
90
91 Token Tok = getTokenAtLoc(Loc, Result, *IdentTable);
92 // Ensure the location actually points to one of the builting integral type
93 // names we're interested in. Otherwise, we might be getting this match from
94 // implicit code (e.g. an implicit assignment operator of a class containing
95 // an array of non-POD types).
96 if (!Tok.isOneOf(tok::kw_short, tok::kw_long, tok::kw_unsigned,
97 tok::kw_signed))
98 return;
99
100 bool IsSigned = false;
101 unsigned Width = 0;
102 const TargetInfo &TargetInfo = Result.Context->getTargetInfo();
103
104 // Look for uses of short, long, long long and their unsigned versions.
105 switch (BuiltinLoc.getTypePtr()->getKind()) {
106 case BuiltinType::Short:
107 Width = TargetInfo.getShortWidth();
108 IsSigned = true;
109 break;
110 case BuiltinType::Long:
111 Width = TargetInfo.getLongWidth();
112 IsSigned = true;
113 break;
114 case BuiltinType::LongLong:
115 Width = TargetInfo.getLongLongWidth();
116 IsSigned = true;
117 break;
118 case BuiltinType::UShort:
119 Width = TargetInfo.getShortWidth();
120 IsSigned = false;
121 break;
122 case BuiltinType::ULong:
123 Width = TargetInfo.getLongWidth();
124 IsSigned = false;
125 break;
126 case BuiltinType::ULongLong:
127 Width = TargetInfo.getLongLongWidth();
128 IsSigned = false;
129 break;
130 default:
131 return;
132 }
133
134 // We allow "unsigned short port" as that's reasonably common and required by
135 // the sockets API.
136 const StringRef Port = "unsigned short port";
137 const char *Data = Result.SourceManager->getCharacterData(Loc);
138 if (!std::strncmp(Data, Port.data(), Port.size()) &&
139 !isAsciiIdentifierContinue(Data[Port.size()]))
140 return;
141
142 std::string Replacement =
143 ((IsSigned ? SignedTypePrefix : UnsignedTypePrefix) + Twine(Width) +
144 TypeSuffix)
145 .str();
146
147 // We don't add a fix-it as changing the type can easily break code,
148 // e.g. when a function requires a 'long' argument on all platforms.
149 // QualTypes are printed with implicit quotes.
150 diag(Loc, "consider replacing %0 with '%1'") << BuiltinLoc.getType()
151 << Replacement;
152}
153
154} // namespace tidy::google::runtime
155} // namespace clang
llvm::SmallString< 256U > Name
FunctionInfo Info
SourceLocation Loc
::clang::DynTypedNode Node
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
IntegerTypesCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static Token getTokenAtLoc(SourceLocation Loc, const MatchFinder::MatchResult &MatchResult, IdentifierTable &IdentTable)
llvm::StringMap< ClangTidyValue > OptionMap