Skip to content

Commit

Permalink
[clang-tidy] add IgnoreMacros option to readability-uppercase-literal…
Browse files Browse the repository at this point in the history
…-suffix

And also enable it by default to be consistent with e.g.
modernize-use-using.

This helps e.g. when running this check on client code where the macro
is provided by the system, so there is no easy way to modify it.

Reviewed By: JonasToth, lebedev.ri

Differential Revision: https://reviews.llvm.org/D56025

llvm-svn: 350056
  • Loading branch information
vmiklos committed Dec 24, 2018
1 parent 4dc3a3f commit 7fb1c2a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,14 @@ UppercaseLiteralSuffixCheck::UppercaseLiteralSuffixCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
NewSuffixes(
utils::options::parseStringList(Options.get("NewSuffixes", ""))) {}
utils::options::parseStringList(Options.get("NewSuffixes", ""))),
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", 1) != 0) {}

void UppercaseLiteralSuffixCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "NewSuffixes",
utils::options::serializeStringList(NewSuffixes));
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}

void UppercaseLiteralSuffixCheck::registerMatchers(MatchFinder *Finder) {
Expand Down Expand Up @@ -216,6 +218,8 @@ bool UppercaseLiteralSuffixCheck::checkBoundMatch(
// We might have a suffix that is already uppercase.
if (auto Details = shouldReplaceLiteralSuffix<LiteralType>(
*Literal, NewSuffixes, *Result.SourceManager, getLangOpts())) {
if (Details->LiteralLocation.getBegin().isMacroID() && IgnoreMacros)
return true;
auto Complaint = diag(Details->LiteralLocation.getBegin(),
"%0 literal has suffix '%1', which is not uppercase")
<< LiteralType::Name << Details->OldSuffix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class UppercaseLiteralSuffixCheck : public ClangTidyCheck {
bool checkBoundMatch(const ast_matchers::MatchFinder::MatchResult &Result);

const std::vector<std::string> NewSuffixes;
const bool IgnoreMacros;
};

} // namespace readability
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ Improvements to clang-tidy
<clang-tidy/checks/readability-redundant-smartptr-get>` check does not warn
about calls inside macros anymore by default.

- The :doc:`readability-uppercase-literal-suffix
<clang-tidy/checks/readability-uppercase-literal-suffix>` check does not warn
about literal suffixes inside macros anymore by default.

- The :doc:`cppcoreguidelines-narrowing-conversions
<clang-tidy/checks/cppcoreguidelines-narrowing-conversions>` check now
detects more narrowing conversions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ Given a list `L;uL`:
* ``uL`` will be kept as is.
* ``ull`` will be kept as is, since it is not in the list
* and so on.

.. option:: IgnoreMacros

If this option is set to non-zero (default is `1`), the check will not warn
about literal suffixes inside macros.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: %check_clang_tidy %s readability-uppercase-literal-suffix %t -- -- -I %S
// RUN: %check_clang_tidy %s readability-uppercase-literal-suffix %t -- \
// RUN: -config="{CheckOptions: [{key: readability-uppercase-literal-suffix.IgnoreMacros, value: 0}]}" \
// RUN: -- -I %S

void macros() {
#define INMACRO(X) 1.f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ void macros() {
// CHECK-FIXES: static constexpr auto m0 = PASSTHROUGH(1U);
static_assert(is_same<decltype(m0), const unsigned int>::value, "");
static_assert(m0 == 1, "");

// This location is inside a macro, no warning on that by default.
#define MACRO 1u
int foo = MACRO;
}

// Check that user-defined literals do not cause any diags.
Expand Down

0 comments on commit 7fb1c2a

Please sign in to comment.