clang-tools 8.0.0

UppercaseLiteralSuffixCheck.cpp
Go to the documentation of this file.
1 //===--- UppercaseLiteralSuffixCheck.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 "UppercaseLiteralSuffixCheck.h"
11 #include "../utils/ASTUtils.h"
12 #include "../utils/OptionsUtils.h"
13 #include "clang/AST/ASTContext.h"
14 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallString.h"
17
18 using namespace clang::ast_matchers;
19
20 namespace clang {
21 namespace tidy {
22 namespace readability {
23
24 namespace {
25
26 struct IntegerLiteralCheck {
27 using type = clang::IntegerLiteral;
28 static constexpr llvm::StringLiteral Name = llvm::StringLiteral("integer");
29 // What should be skipped before looking for the Suffixes? (Nothing here.)
30 static constexpr llvm::StringLiteral SkipFirst = llvm::StringLiteral("");
31 // Suffix can only consist of 'u' and 'l' chars, and can be a complex number
32 // ('i', 'j'). In MS compatibility mode, suffixes like i32 are supported.
33 static constexpr llvm::StringLiteral Suffixes =
34 llvm::StringLiteral("uUlLiIjJ");
35 };
36 constexpr llvm::StringLiteral IntegerLiteralCheck::Name;
37 constexpr llvm::StringLiteral IntegerLiteralCheck::SkipFirst;
38 constexpr llvm::StringLiteral IntegerLiteralCheck::Suffixes;
39
40 struct FloatingLiteralCheck {
41 using type = clang::FloatingLiteral;
42 static constexpr llvm::StringLiteral Name =
43 llvm::StringLiteral("floating point");
44 // C++17 introduced hexadecimal floating-point literals, and 'f' is both a
45 // valid hexadecimal digit in a hex float literal and a valid floating-point
46 // literal suffix.
47 // So we can't just "skip to the chars that can be in the suffix".
48 // Since the exponent ('p'/'P') is mandatory for hexadecimal floating-point
49 // literals, we first skip everything before the exponent.
50 static constexpr llvm::StringLiteral SkipFirst = llvm::StringLiteral("pP");
51 // Suffix can only consist of 'f', 'l', "f16", 'h', 'q' chars,
52 // and can be a complex number ('i', 'j').
53 static constexpr llvm::StringLiteral Suffixes =
54 llvm::StringLiteral("fFlLhHqQiIjJ");
55 };
56 constexpr llvm::StringLiteral FloatingLiteralCheck::Name;
57 constexpr llvm::StringLiteral FloatingLiteralCheck::SkipFirst;
58 constexpr llvm::StringLiteral FloatingLiteralCheck::Suffixes;
59
60 struct NewSuffix {
61 SourceRange LiteralLocation;
62 StringRef OldSuffix;
63 llvm::Optional<FixItHint> FixIt;
64 };
65
66 llvm::Optional<SourceLocation> GetMacroAwareLocation(SourceLocation Loc,
67 const SourceManager &SM) {
68 // Do nothing if the provided location is invalid.
69 if (Loc.isInvalid())
70 return llvm::None;
71 // Look where the location was *actually* written.
72 SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
73 if (SpellingLoc.isInvalid())
74 return llvm::None;
75 return SpellingLoc;
76 }
77
78 llvm::Optional<SourceRange> GetMacroAwareSourceRange(SourceRange Loc,
79 const SourceManager &SM) {
80 llvm::Optional<SourceLocation> Begin =
81 GetMacroAwareLocation(Loc.getBegin(), SM);
82 llvm::Optional<SourceLocation> End = GetMacroAwareLocation(Loc.getEnd(), SM);
83 if (!Begin || !End)
84 return llvm::None;
85 return SourceRange(*Begin, *End);
86 }
87
88 llvm::Optional<std::string>
89 getNewSuffix(llvm::StringRef OldSuffix,
90 const std::vector<std::string> &NewSuffixes) {
91 // If there is no config, just uppercase the entirety of the suffix.
92 if (NewSuffixes.empty())
93 return OldSuffix.upper();
94 // Else, find matching suffix, case-*insensitive*ly.
95 auto NewSuffix = llvm::find_if(
96 NewSuffixes, [OldSuffix](const std::string &PotentialNewSuffix) {
97 return OldSuffix.equals_lower(PotentialNewSuffix);
98 });
99 // Have a match, return it.
100 if (NewSuffix != NewSuffixes.end())
101 return *NewSuffix;
102 // Nope, I guess we have to keep it as-is.
103 return llvm::None;
104 }
105
106 template <typename LiteralType>
107 llvm::Optional<NewSuffix>
108 shouldReplaceLiteralSuffix(const Expr &Literal,
109 const std::vector<std::string> &NewSuffixes,
110 const SourceManager &SM, const LangOptions &LO) {
111 NewSuffix ReplacementDsc;
112
113 const auto &L = cast<typename LiteralType::type>(Literal);
114
115 // The naive location of the literal. Is always valid.
116 ReplacementDsc.LiteralLocation = L.getSourceRange();
117
118 // Was this literal fully spelled or is it a product of macro expansion?
119 bool RangeCanBeFixed =
120 utils::rangeCanBeFixed(ReplacementDsc.LiteralLocation, &SM);
121
122 // The literal may have macro expansion, we need the final expanded src range.
123 llvm::Optional<SourceRange> Range =
124 GetMacroAwareSourceRange(ReplacementDsc.LiteralLocation, SM);
125 if (!Range)
126 return llvm::None;
127
128 if (RangeCanBeFixed)
129 ReplacementDsc.LiteralLocation = *Range;
130 // Else keep the naive literal location!
131
132 // Get the whole literal from the source buffer.
133 bool Invalid;
134 const StringRef LiteralSourceText = Lexer::getSourceText(
135 CharSourceRange::getTokenRange(*Range), SM, LO, &Invalid);
136 assert(!Invalid && "Failed to retrieve the source text.");
137
138 size_t Skip = 0;
139
140 // Do we need to ignore something before actually looking for the suffix?
141 if (!LiteralType::SkipFirst.empty()) {
142 // E.g. we can't look for 'f' suffix in hexadecimal floating-point literals
143 // until after we skip to the exponent (which is mandatory there),
144 // because hex-digit-sequence may contain 'f'.
145 Skip = LiteralSourceText.find_first_of(LiteralType::SkipFirst);
146 // We could be in non-hexadecimal floating-point literal, with no exponent.
147 if (Skip == StringRef::npos)
148 Skip = 0;
149 }
150
151 // Find the beginning of the suffix by looking for the first char that is
152 // one of these chars that can be in the suffix, potentially starting looking
153 // in the exponent, if we are skipping hex-digit-sequence.
154 Skip = LiteralSourceText.find_first_of(LiteralType::Suffixes, /*From=*/Skip);
155
156 // We can't check whether the *Literal has any suffix or not without actually
157 // looking for the suffix. So it is totally possible that there is no suffix.
158 if (Skip == StringRef::npos)
159 return llvm::None;
160
161 // Move the cursor in the source range to the beginning of the suffix.
162 Range->setBegin(Range->getBegin().getLocWithOffset(Skip));
163 // And in our textual representation too.
164 ReplacementDsc.OldSuffix = LiteralSourceText.drop_front(Skip);
165 assert(!ReplacementDsc.OldSuffix.empty() &&
166 "We still should have some chars left.");
167
168 // And get the replacement suffix.
169 llvm::Optional<std::string> NewSuffix =
170 getNewSuffix(ReplacementDsc.OldSuffix, NewSuffixes);
171 if (!NewSuffix || ReplacementDsc.OldSuffix == *NewSuffix)
172 return llvm::None; // The suffix was already the way it should be.
173
174 if (RangeCanBeFixed)
175 ReplacementDsc.FixIt = FixItHint::CreateReplacement(*Range, *NewSuffix);
176
177 return ReplacementDsc;
178 }
179
180 } // namespace
181
182 UppercaseLiteralSuffixCheck::UppercaseLiteralSuffixCheck(
183 StringRef Name, ClangTidyContext *Context)
184 : ClangTidyCheck(Name, Context),
185 NewSuffixes(
186 utils::options::parseStringList(Options.get("NewSuffixes", ""))),
187 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", 1) != 0) {}
188
189 void UppercaseLiteralSuffixCheck::storeOptions(
190 ClangTidyOptions::OptionMap &Opts) {
191 Options.store(Opts, "NewSuffixes",
192 utils::options::serializeStringList(NewSuffixes));
193 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
194 }
195
196 void UppercaseLiteralSuffixCheck::registerMatchers(MatchFinder *Finder) {
197 // Sadly, we can't check whether the literal has sufix or not.
198 // E.g. i32 suffix still results in 'BuiltinType::Kind::Int'.
199 // And such an info is not stored in the *Literal itself.
200 Finder->addMatcher(
201 stmt(eachOf(integerLiteral().bind(IntegerLiteralCheck::Name),
202 floatLiteral().bind(FloatingLiteralCheck::Name)),
203 unless(anyOf(hasParent(userDefinedLiteral()),
204 hasAncestor(isImplicit()),
205 hasAncestor(substNonTypeTemplateParmExpr())))),
206 this);
207 }
208
209 template <typename LiteralType>
210 bool UppercaseLiteralSuffixCheck::checkBoundMatch(
211 const MatchFinder::MatchResult &Result) {
212 const auto *Literal =
213 Result.Nodes.getNodeAs<typename LiteralType::type>(LiteralType::Name);
214 if (!Literal)
215 return false;
216
217 // We won't *always* want to diagnose.
218 // We might have a suffix that is already uppercase.
219 if (auto Details = shouldReplaceLiteralSuffix<LiteralType>(
220 *Literal, NewSuffixes, *Result.SourceManager, getLangOpts())) {
221 if (Details->LiteralLocation.getBegin().isMacroID() && IgnoreMacros)
222 return true;
223 auto Complaint = diag(Details->LiteralLocation.getBegin(),
224 "%0 literal has suffix '%1', which is not uppercase")
225 << LiteralType::Name << Details->OldSuffix;
226 if (Details->FixIt) // Similarly, a fix-it is not always possible.
227 Complaint << *(Details->FixIt);
228 }
229
230 return true;
231 }
232
233 void UppercaseLiteralSuffixCheck::check(
234 const MatchFinder::MatchResult &Result) {
235 if (checkBoundMatch<IntegerLiteralCheck>(Result))
236 return; // If it *was* IntegerLiteral, don't check for FloatingLiteral.
237 checkBoundMatch<FloatingLiteralCheck>(Result);
238 }
239
240 } // namespace readability
241 } // namespace tidy
242 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::ast_matchers
Definition: AbseilMatcher.h:15
clang::tidy::readability::UppercaseLiteralSuffixCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: UppercaseLiteralSuffixCheck.cpp:233
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:473
clang::tidy::utils::options::serializeStringList
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
Definition: OptionsUtils.cpp:31
clang::tidy::ClangTidyCheck::getLangOpts
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang::tidy::readability::UppercaseLiteralSuffixCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UppercaseLiteralSuffixCheck.cpp:196
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:19
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:183
LiteralLocation
SourceRange LiteralLocation
Definition: UppercaseLiteralSuffixCheck.cpp:61
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:98
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
SkipFirst
static constexpr llvm::StringLiteral SkipFirst
Definition: UppercaseLiteralSuffixCheck.cpp:30
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
OldSuffix
StringRef OldSuffix
Definition: UppercaseLiteralSuffixCheck.cpp:62
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::tidy::utils::rangeCanBeFixed
bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:91
UppercaseLiteralSuffixCheck.h
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:100
FixIt
llvm::Optional< FixItHint > FixIt
Definition: UppercaseLiteralSuffixCheck.cpp:63
clang::tidy::readability::UppercaseLiteralSuffixCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: UppercaseLiteralSuffixCheck.cpp:189
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438
Suffixes
static constexpr llvm::StringLiteral Suffixes
Definition: UppercaseLiteralSuffixCheck.cpp:33