clang-tools  10.0.0svn
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 "../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 
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_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>
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", 1) != 0) {}
187 
190  Options.store(Opts, "NewSuffixes",
192  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
193 }
194 
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>
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 
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
SourceLocation Loc
&#39;#&#39; location in the include directive
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
Base class for all clang-tidy checks.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
const LangOptions & getLangOpts() const
Returns the language options from the context.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
SourceRange LiteralLocation
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:203
static constexpr llvm::StringLiteral SkipFirst
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
StringRef OldSuffix
CharSourceRange Range
SourceRange for the file name.
bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:90
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36
llvm::Optional< FixItHint > FixIt
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
static constexpr llvm::StringLiteral Suffixes