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