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 "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/SmallString.h"
16 #include <cctype>
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 
getMacroAwareLocation(SourceLocation Loc,const SourceManager & SM)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 
getMacroAwareSourceRange(SourceRange Loc,const SourceManager & SM)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>
getNewSuffix(llvm::StringRef OldSuffix,const std::vector<StringRef> & NewSuffixes)89 getNewSuffix(llvm::StringRef OldSuffix,
90              const std::vector<StringRef> &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 =
96       llvm::find_if(NewSuffixes, [OldSuffix](StringRef PotentialNewSuffix) {
97         return OldSuffix.equals_insensitive(PotentialNewSuffix);
98       });
99   // Have a match, return it.
100   if (NewSuffix != NewSuffixes.end())
101     return NewSuffix->str();
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>
shouldReplaceLiteralSuffix(const Expr & Literal,const std::vector<StringRef> & NewSuffixes,const SourceManager & SM,const LangOptions & LO)108 shouldReplaceLiteralSuffix(const Expr &Literal,
109                            const std::vector<StringRef> &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   // Make sure the first character is actually a digit, instead of
139   // something else, like a non-type template parameter.
140   if (!std::isdigit(static_cast<unsigned char>(LiteralSourceText.front())))
141     return llvm::None;
142 
143   size_t Skip = 0;
144 
145   // Do we need to ignore something before actually looking for the suffix?
146   if (!LiteralType::SkipFirst.empty()) {
147     // E.g. we can't look for 'f' suffix in hexadecimal floating-point literals
148     // until after we skip to the exponent (which is mandatory there),
149     // because hex-digit-sequence may contain 'f'.
150     Skip = LiteralSourceText.find_first_of(LiteralType::SkipFirst);
151     // We could be in non-hexadecimal floating-point literal, with no exponent.
152     if (Skip == StringRef::npos)
153       Skip = 0;
154   }
155 
156   // Find the beginning of the suffix by looking for the first char that is
157   // one of these chars that can be in the suffix, potentially starting looking
158   // in the exponent, if we are skipping hex-digit-sequence.
159   Skip = LiteralSourceText.find_first_of(LiteralType::Suffixes, /*From=*/Skip);
160 
161   // We can't check whether the *Literal has any suffix or not without actually
162   // looking for the suffix. So it is totally possible that there is no suffix.
163   if (Skip == StringRef::npos)
164     return llvm::None;
165 
166   // Move the cursor in the source range to the beginning of the suffix.
167   Range->setBegin(Range->getBegin().getLocWithOffset(Skip));
168   // And in our textual representation too.
169   ReplacementDsc.OldSuffix = LiteralSourceText.drop_front(Skip);
170   assert(!ReplacementDsc.OldSuffix.empty() &&
171          "We still should have some chars left.");
172 
173   // And get the replacement suffix.
174   llvm::Optional<std::string> NewSuffix =
175       getNewSuffix(ReplacementDsc.OldSuffix, NewSuffixes);
176   if (!NewSuffix || ReplacementDsc.OldSuffix == *NewSuffix)
177     return llvm::None; // The suffix was already the way it should be.
178 
179   if (RangeCanBeFixed)
180     ReplacementDsc.FixIt = FixItHint::CreateReplacement(*Range, *NewSuffix);
181 
182   return ReplacementDsc;
183 }
184 
185 } // namespace
186 
UppercaseLiteralSuffixCheck(StringRef Name,ClangTidyContext * Context)187 UppercaseLiteralSuffixCheck::UppercaseLiteralSuffixCheck(
188     StringRef Name, ClangTidyContext *Context)
189     : ClangTidyCheck(Name, Context),
190       NewSuffixes(
191           utils::options::parseStringList(Options.get("NewSuffixes", ""))),
192       IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
193 
storeOptions(ClangTidyOptions::OptionMap & Opts)194 void UppercaseLiteralSuffixCheck::storeOptions(
195     ClangTidyOptions::OptionMap &Opts) {
196   Options.store(Opts, "NewSuffixes",
197                 utils::options::serializeStringList(NewSuffixes));
198   Options.store(Opts, "IgnoreMacros", IgnoreMacros);
199 }
200 
registerMatchers(MatchFinder * Finder)201 void UppercaseLiteralSuffixCheck::registerMatchers(MatchFinder *Finder) {
202   // Sadly, we can't check whether the literal has suffix or not.
203   // E.g. i32 suffix still results in 'BuiltinType::Kind::Int'.
204   // And such an info is not stored in the *Literal itself.
205   Finder->addMatcher(
206       stmt(eachOf(integerLiteral().bind(IntegerLiteralCheck::Name),
207                   floatLiteral().bind(FloatingLiteralCheck::Name)),
208            unless(anyOf(hasParent(userDefinedLiteral()),
209                         hasAncestor(substNonTypeTemplateParmExpr())))),
210       this);
211 }
212 
213 template <typename LiteralType>
checkBoundMatch(const MatchFinder::MatchResult & Result)214 bool UppercaseLiteralSuffixCheck::checkBoundMatch(
215     const MatchFinder::MatchResult &Result) {
216   const auto *Literal =
217       Result.Nodes.getNodeAs<typename LiteralType::type>(LiteralType::Name);
218   if (!Literal)
219     return false;
220 
221   // We won't *always* want to diagnose.
222   // We might have a suffix that is already uppercase.
223   if (auto Details = shouldReplaceLiteralSuffix<LiteralType>(
224           *Literal, NewSuffixes, *Result.SourceManager, getLangOpts())) {
225     if (Details->LiteralLocation.getBegin().isMacroID() && IgnoreMacros)
226       return true;
227     auto Complaint = diag(Details->LiteralLocation.getBegin(),
228                           "%0 literal has suffix '%1', which is not uppercase")
229                      << LiteralType::Name << Details->OldSuffix;
230     if (Details->FixIt) // Similarly, a fix-it is not always possible.
231       Complaint << *(Details->FixIt);
232   }
233 
234   return true;
235 }
236 
check(const MatchFinder::MatchResult & Result)237 void UppercaseLiteralSuffixCheck::check(
238     const MatchFinder::MatchResult &Result) {
239   if (checkBoundMatch<IntegerLiteralCheck>(Result))
240     return; // If it *was* IntegerLiteral, don't check for FloatingLiteral.
241   checkBoundMatch<FloatingLiteralCheck>(Result);
242 }
243 
244 } // namespace readability
245 } // namespace tidy
246 } // namespace clang
247