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 "clang/Lex/Preprocessor.h" 13 #include "llvm/ADT/STLExtras.h" 14 #include "llvm/Support/Regex.h" 15 #include <algorithm> 16 #include <cctype> 17 18 namespace clang { 19 namespace tidy { 20 namespace cppcoreguidelines { 21 22 namespace { 23 24 bool isCapsOnly(StringRef Name) { 25 return std::all_of(Name.begin(), Name.end(), [](const char C) { 26 if (std::isupper(C) || std::isdigit(C) || C == '_') 27 return true; 28 return false; 29 }); 30 } 31 32 class MacroUsageCallbacks : public PPCallbacks { 33 public: 34 MacroUsageCallbacks(MacroUsageCheck *Check, const SourceManager &SM, 35 StringRef RegExpStr, bool CapsOnly, bool IgnoreCommandLine) 36 : Check(Check), SM(SM), RegExp(RegExpStr), CheckCapsOnly(CapsOnly), 37 IgnoreCommandLineMacros(IgnoreCommandLine) {} 38 void MacroDefined(const Token &MacroNameTok, 39 const MacroDirective *MD) override { 40 if (SM.isWrittenInBuiltinFile(MD->getLocation()) || 41 MD->getMacroInfo()->isUsedForHeaderGuard() || 42 MD->getMacroInfo()->getNumTokens() == 0) 43 return; 44 45 if (IgnoreCommandLineMacros && 46 SM.isWrittenInCommandLineFile(MD->getLocation())) 47 return; 48 49 StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName(); 50 if (MacroName == "__GCC_HAVE_DWARF2_CFI_ASM") 51 return; 52 if (!CheckCapsOnly && !RegExp.match(MacroName)) 53 Check->warnMacro(MD, MacroName); 54 55 if (CheckCapsOnly && !isCapsOnly(MacroName)) 56 Check->warnNaming(MD, MacroName); 57 } 58 59 private: 60 MacroUsageCheck *Check; 61 const SourceManager &SM; 62 const llvm::Regex RegExp; 63 bool CheckCapsOnly; 64 bool IgnoreCommandLineMacros; 65 }; 66 } // namespace 67 68 void MacroUsageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { 69 Options.store(Opts, "AllowedRegexp", AllowedRegexp); 70 Options.store(Opts, "CheckCapsOnly", CheckCapsOnly); 71 Options.store(Opts, "IgnoreCommandLineMacros", IgnoreCommandLineMacros); 72 } 73 74 void MacroUsageCheck::registerPPCallbacks(const SourceManager &SM, 75 Preprocessor *PP, 76 Preprocessor *ModuleExpanderPP) { 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