UppercaseLiteralSuffixCheck.cpp 9.4 KB

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