blob: 588fb2615565e7c4ada614cbec4b98b64ea52e77 [file] [log] [blame]
Roman Lebedev08701ec2018-10-26 13:09:27 +00001//===--- 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
18using namespace clang::ast_matchers;
19
20namespace clang {
21namespace tidy {
22namespace readability {
23
24namespace {
25
26struct 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};
36constexpr llvm::StringLiteral IntegerLiteralCheck::Name;
37constexpr llvm::StringLiteral IntegerLiteralCheck::SkipFirst;
38constexpr llvm::StringLiteral IntegerLiteralCheck::Suffixes;
39
40struct 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};
56constexpr llvm::StringLiteral FloatingLiteralCheck::Name;
57constexpr llvm::StringLiteral FloatingLiteralCheck::SkipFirst;
58constexpr llvm::StringLiteral FloatingLiteralCheck::Suffixes;
59
60struct NewSuffix {
61 SourceRange LiteralLocation;
62 StringRef OldSuffix;
63 llvm::Optional<FixItHint> FixIt;
64};
65
66llvm::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
78llvm::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
88llvm::Optional<std::string>
89getNewSuffix(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
106template <typename LiteralType>
107llvm::Optional<NewSuffix>
108shouldReplaceLiteralSuffix(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
182UppercaseLiteralSuffixCheck::UppercaseLiteralSuffixCheck(
183 StringRef Name, ClangTidyContext *Context)
184 : ClangTidyCheck(Name, Context),
185 NewSuffixes(
Miklos Vajna7fb1c2a2018-12-24 17:47:32 +0000186 utils::options::parseStringList(Options.get("NewSuffixes", ""))),
187 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", 1) != 0) {}
Roman Lebedev08701ec2018-10-26 13:09:27 +0000188
189void UppercaseLiteralSuffixCheck::storeOptions(
190 ClangTidyOptions::OptionMap &Opts) {
191 Options.store(Opts, "NewSuffixes",
192 utils::options::serializeStringList(NewSuffixes));
Miklos Vajna7fb1c2a2018-12-24 17:47:32 +0000193 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
Roman Lebedev08701ec2018-10-26 13:09:27 +0000194}
195
196void 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(
Alexander Kornienko976e0c02018-11-25 02:41:01 +0000201 stmt(eachOf(integerLiteral().bind(IntegerLiteralCheck::Name),
202 floatLiteral().bind(FloatingLiteralCheck::Name)),
203 unless(anyOf(hasParent(userDefinedLiteral()),
204 hasAncestor(isImplicit()),
205 hasAncestor(substNonTypeTemplateParmExpr())))),
Roman Lebedev08701ec2018-10-26 13:09:27 +0000206 this);
207}
208
209template <typename LiteralType>
210bool 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())) {
Miklos Vajna7fb1c2a2018-12-24 17:47:32 +0000221 if (Details->LiteralLocation.getBegin().isMacroID() && IgnoreMacros)
222 return true;
Roman Lebedev08701ec2018-10-26 13:09:27 +0000223 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
233void 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