1 //===--- MacroUsageCheck.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 9 #include "MacroUsageCheck.h" 10 #include "clang/Frontend/CompilerInstance.h" 11 #include "clang/Lex/PPCallbacks.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/Support/Regex.h" 14 #include <algorithm> 15 #include <cctype> 16 17 namespace clang { 18 namespace tidy { 19 namespace cppcoreguidelines { 20 21 namespace { 22 23 bool isCapsOnly(StringRef Name) { 24 return std::all_of(Name.begin(), Name.end(), [](const char c) { 25 if (std::isupper(c) || std::isdigit(c) || c == '_') 26 return true; 27 return false; 28 }); 29 } 30 31 class MacroUsageCallbacks : public PPCallbacks { 32 public: 33 MacroUsageCallbacks(MacroUsageCheck *Check, const SourceManager &SM, 34 StringRef RegExp, bool CapsOnly, bool IgnoreCommandLine) 35 : Check(Check), SM(SM), RegExp(RegExp), CheckCapsOnly(CapsOnly), 36 IgnoreCommandLineMacros(IgnoreCommandLine) {} 37 void MacroDefined(const Token &MacroNameTok, 38 const MacroDirective *MD) override { 39 if (SM.isWrittenInBuiltinFile(MD->getLocation()) || 40 MD->getMacroInfo()->isUsedForHeaderGuard() || 41 MD->getMacroInfo()->getNumTokens() == 0) 42 return; 43 44 if (IgnoreCommandLineMacros && 45 SM.isWrittenInCommandLineFile(MD->getLocation())) 46 return; 47 48 StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName(); 49 if (!CheckCapsOnly && !llvm::Regex(RegExp).match(MacroName)) 50 Check->warnMacro(MD, MacroName); 51 52 if (CheckCapsOnly && !isCapsOnly(MacroName)) 53 Check->warnNaming(MD, MacroName); 54 } 55 56 private: 57 MacroUsageCheck *Check; 58 const SourceManager &SM; 59 StringRef RegExp; 60 bool CheckCapsOnly; 61 bool IgnoreCommandLineMacros; 62 }; 63 } // namespace 64 65 void MacroUsageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { 66 Options.store(Opts, "AllowedRegexp", AllowedRegexp); 67 Options.store(Opts, "CheckCapsOnly", CheckCapsOnly); 68 Options.store(Opts, "IgnoreCommandLineMacros", IgnoreCommandLineMacros); 69 } 70 71 void MacroUsageCheck::registerPPCallbacks(const SourceManager &SM, 72 Preprocessor *PP, 73 Preprocessor *ModuleExpanderPP) { 74 if (!getLangOpts().CPlusPlus11) 75 return; 76 77 PP->addPPCallbacks(std::make_unique<MacroUsageCallbacks>( 78 this, SM, AllowedRegexp, CheckCapsOnly, IgnoreCommandLineMacros)); 79 } 80 81 void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) { 82 StringRef Message = 83 "macro '%0' used to declare a constant; consider using a 'constexpr' " 84 "constant"; 85 86 /// A variadic macro is function-like at the same time. Therefore variadic 87 /// macros are checked first and will be excluded for the function-like 88 /// diagnostic. 89 if (MD->getMacroInfo()->isVariadic()) 90 Message = "variadic macro '%0' used; consider using a 'constexpr' " 91 "variadic template function"; 92 else if (MD->getMacroInfo()->isFunctionLike()) 93 Message = "function-like macro '%0' used; consider a 'constexpr' template " 94 "function"; 95 96 diag(MD->getLocation(), Message) << MacroName; 97 } 98 99 void MacroUsageCheck::warnNaming(const MacroDirective *MD, 100 StringRef MacroName) { 101 diag(MD->getLocation(), "macro definition does not define the macro name " 102 "'%0' using all uppercase characters") 103 << MacroName; 104 } 105 106 } // namespace cppcoreguidelines 107 } // namespace tidy 108 } // namespace clang 109