clang-tools  14.0.0git
UppercaseLiteralSuffixCheck.cpp
Go to the documentation of this file.
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 
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 
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 
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 
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>
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_insensitive(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>
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 
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", true)) {}
187 
190  Options.store(Opts, "NewSuffixes",
192  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
193 }
194 
196  // Sadly, we can't check whether the literal has suffix 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(substNonTypeTemplateParmExpr())))),
204  this);
205 }
206 
207 template <typename LiteralType>
208 bool UppercaseLiteralSuffixCheck::checkBoundMatch(
209  const MatchFinder::MatchResult &Result) {
210  const auto *Literal =
211  Result.Nodes.getNodeAs<typename LiteralType::type>(LiteralType::Name);
212  if (!Literal)
213  return false;
214 
215  // We won't *always* want to diagnose.
216  // We might have a suffix that is already uppercase.
217  if (auto Details = shouldReplaceLiteralSuffix<LiteralType>(
218  *Literal, NewSuffixes, *Result.SourceManager, getLangOpts())) {
219  if (Details->LiteralLocation.getBegin().isMacroID() && IgnoreMacros)
220  return true;
221  auto Complaint = diag(Details->LiteralLocation.getBegin(),
222  "%0 literal has suffix '%1', which is not uppercase")
223  << LiteralType::Name << Details->OldSuffix;
224  if (Details->FixIt) // Similarly, a fix-it is not always possible.
225  Complaint << *(Details->FixIt);
226  }
227 
228  return true;
229 }
230 
232  const MatchFinder::MatchResult &Result) {
233  if (checkBoundMatch<IntegerLiteralCheck>(Result))
234  return; // If it *was* IntegerLiteral, don't check for FloatingLiteral.
235  checkBoundMatch<FloatingLiteralCheck>(Result);
236 }
237 
238 } // namespace readability
239 } // namespace tidy
240 } // namespace clang
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:231
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
LiteralLocation
SourceRange LiteralLocation
Definition: UppercaseLiteralSuffixCheck.cpp:60
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::cppcoreguidelines::getSourceText
static std::string getSourceText(const CXXDestructorDecl &Destructor)
Definition: VirtualClassDestructorCheck.cpp:96
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
OldSuffix
StringRef OldSuffix
Definition: UppercaseLiteralSuffixCheck.cpp:61
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:420
clang::ast_matchers
Definition: AbseilMatcher.h:14
FixIt
llvm::Optional< FixItHint > FixIt
Definition: UppercaseLiteralSuffixCheck.cpp:62
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:30
clang::tidy::readability::UppercaseLiteralSuffixCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: UppercaseLiteralSuffixCheck.cpp:195
clang::tidy::bugprone::model::MixFlags::Invalid
@ Invalid
Sentinel bit pattern. DO NOT USE!
SkipFirst
static constexpr llvm::StringLiteral SkipFirst
Definition: UppercaseLiteralSuffixCheck.cpp:29
clang::tidy::utils::options::parseStringList
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
Definition: OptionsUtils.cpp:18
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
Suffixes
static constexpr llvm::StringLiteral Suffixes
Definition: UppercaseLiteralSuffixCheck.cpp:32
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::rangeCanBeFixed
bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:88
clang::tidy::ClangTidyCheck::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: ClangTidyCheck.cpp:120
UppercaseLiteralSuffixCheck.h
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:188
Details
SmallVector< Detail, DefaultLimit > Details
Definition: FunctionCognitiveComplexityCheck.cpp:138