lebedev.ri created this revision. lebedev.ri added reviewers: JonasToth, aaron.ballman, hokein, xazax.hun, alexfh. lebedev.ri added a project: clang-tools-extra. Herald added subscribers: rnkovacs, kbarton, nemanjai.
The macro may not have location (or more generally, the location may not exist), e.g. if it originates from compiler's command-line. The check complains on all the macros, even those without the location info. Which means, it only says it does not like it. What is 'it'? I have no idea. If we don't print the name, then there is no way to deal with that situation. And in general, not printing name here forces the user to try to understand, given, the macro definition location, what is the macro name? This isn't fun. I suspect some more issues may crop up later. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D53817 Files: clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp clang-tidy/cppcoreguidelines/MacroUsageCheck.h test/clang-tidy/cppcoreguidelines-macro-usage-caps-only.cpp test/clang-tidy/cppcoreguidelines-macro-usage-custom.cpp test/clang-tidy/cppcoreguidelines-macro-usage.cpp
Index: test/clang-tidy/cppcoreguidelines-macro-usage.cpp =================================================================== --- test/clang-tidy/cppcoreguidelines-macro-usage.cpp +++ test/clang-tidy/cppcoreguidelines-macro-usage.cpp @@ -4,15 +4,15 @@ #define INCLUDE_GUARD #define PROBLEMATIC_CONSTANT 0 -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro used to declare a constant; consider using a 'constexpr' constant +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro 'PROBLEMATIC_CONSTANT' used to declare a constant; consider using a 'constexpr' constant #define PROBLEMATIC_FUNCTION(x, y) ((a) > (b) ? (a) : (b)) -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: function-like macro used; consider a 'constexpr' template function +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: function-like macro 'PROBLEMATIC_FUNCTION' used; consider a 'constexpr' template function #define PROBLEMATIC_VARIADIC(...) (__VA_ARGS__) -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro used; consider using a 'constexpr' variadic template function +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro 'PROBLEMATIC_VARIADIC' used; consider using a 'constexpr' variadic template function #define PROBLEMATIC_VARIADIC2(x, ...) (__VA_ARGS__) -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro used; consider using a 'constexpr' variadic template function +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro 'PROBLEMATIC_VARIADIC2' used; consider using a 'constexpr' variadic template function #endif Index: test/clang-tidy/cppcoreguidelines-macro-usage-custom.cpp =================================================================== --- test/clang-tidy/cppcoreguidelines-macro-usage-custom.cpp +++ test/clang-tidy/cppcoreguidelines-macro-usage-custom.cpp @@ -6,16 +6,16 @@ #define INCLUDE_GUARD #define PROBLEMATIC_CONSTANT 0 -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro used to declare a constant; consider using a 'constexpr' constant +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro 'PROBLEMATIC_CONSTANT' used to declare a constant; consider using a 'constexpr' constant #define PROBLEMATIC_FUNCTION(x, y) ((a) > (b) ? (a) : (b)) -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: function-like macro used; consider a 'constexpr' template function +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: function-like macro 'PROBLEMATIC_FUNCTION' used; consider a 'constexpr' template function #define PROBLEMATIC_VARIADIC(...) (__VA_ARGS__) -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro used; consider using a 'constexpr' variadic template function +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro 'PROBLEMATIC_VARIADIC' used; consider using a 'constexpr' variadic template function #define PROBLEMATIC_VARIADIC2(x, ...) (__VA_ARGS__) -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro used; consider using a 'constexpr' variadic template function +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: variadic macro 'PROBLEMATIC_VARIADIC2' used; consider using a 'constexpr' variadic template function #define DEBUG_CONSTANT 0 #define DEBUG_FUNCTION(x, y) ((a) > (b) ? (a) : (b)) Index: test/clang-tidy/cppcoreguidelines-macro-usage-caps-only.cpp =================================================================== --- test/clang-tidy/cppcoreguidelines-macro-usage-caps-only.cpp +++ test/clang-tidy/cppcoreguidelines-macro-usage-caps-only.cpp @@ -6,16 +6,16 @@ #define INCLUDE_GUARD #define problematic_constant 0 -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro definition does not define the macro name using all uppercase characters +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro definition does not define the macro name 'problematic_constant' using all uppercase characters #define problematic_function(x, y) ((a) > (b) ? (a) : (b)) -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro definition does not define the macro name using all uppercase characters +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro definition does not define the macro name 'problematic_function' using all uppercase characters #define problematic_variadic(...) (__VA_ARGS__) -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro definition does not define the macro name using all uppercase characters +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro definition does not define the macro name 'problematic_variadic' using all uppercase characters // #define problematic_variadic2(x, ...) (__VA_ARGS__) -// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro definition does not define the macro name using all uppercase characters +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: macro definition does not define the macro name 'problematic_variadic2' using all uppercase characters #define OKISH_CONSTANT 42 #define OKISH_FUNCTION(x, y) ((a) > (b) ? (a) : (b)) Index: clang-tidy/cppcoreguidelines/MacroUsageCheck.h =================================================================== --- clang-tidy/cppcoreguidelines/MacroUsageCheck.h +++ clang-tidy/cppcoreguidelines/MacroUsageCheck.h @@ -31,8 +31,8 @@ CheckCapsOnly(Options.get("CheckCapsOnly", 0)) {} void storeOptions(ClangTidyOptions::OptionMap &Opts) override; void registerPPCallbacks(CompilerInstance &Compiler) override; - void warnMacro(const MacroDirective *MD); - void warnNaming(const MacroDirective *MD); + void warnMacro(const MacroDirective *MD, StringRef MacroName); + void warnNaming(const MacroDirective *MD, StringRef MacroName); private: /// A regular expression that defines how allowed macros must look like. Index: clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp =================================================================== --- clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp +++ clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp @@ -41,10 +41,10 @@ StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName(); if (!CheckCapsOnly && !llvm::Regex(RegExp).match(MacroName)) - Check->warnMacro(MD); + Check->warnMacro(MD, MacroName); if (CheckCapsOnly && !isCapsOnly(MacroName)) - Check->warnNaming(MD); + Check->warnNaming(MD, MacroName); } private: @@ -68,27 +68,29 @@ CheckCapsOnly)); } -void MacroUsageCheck::warnMacro(const MacroDirective *MD) { +void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) { StringRef Message = - "macro used to declare a constant; consider using a 'constexpr' " + "macro '%0' used to declare a constant; consider using a 'constexpr' " "constant"; /// A variadic macro is function-like at the same time. Therefore variadic /// macros are checked first and will be excluded for the function-like /// diagnostic. if (MD->getMacroInfo()->isVariadic()) - Message = "variadic macro used; consider using a 'constexpr' " + Message = "variadic macro '%0' used; consider using a 'constexpr' " "variadic template function"; else if (MD->getMacroInfo()->isFunctionLike()) - Message = "function-like macro used; consider a 'constexpr' template " + Message = "function-like macro '%0' used; consider a 'constexpr' template " "function"; - diag(MD->getLocation(), Message); + diag(MD->getLocation(), Message) << MacroName; } -void MacroUsageCheck::warnNaming(const MacroDirective *MD) { +void MacroUsageCheck::warnNaming(const MacroDirective *MD, + StringRef MacroName) { diag(MD->getLocation(), "macro definition does not define the macro name " - "using all uppercase characters"); + "'%0' using all uppercase characters") + << MacroName; } } // namespace cppcoreguidelines
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits