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 PP->addPPCallbacks(std::make_unique<MacroUsageCallbacks>( 75 this, SM, AllowedRegexp, CheckCapsOnly, IgnoreCommandLineMacros)); 76 } 77 78 void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) { 79 StringRef Message = 80 "macro '%0' used to declare a constant; consider using a 'constexpr' " 81 "constant"; 82 83 /// A variadic macro is function-like at the same time. Therefore variadic 84 /// macros are checked first and will be excluded for the function-like 85 /// diagnostic. 86 if (MD->getMacroInfo()->isVariadic()) 87 Message = "variadic macro '%0' used; consider using a 'constexpr' " 88 "variadic template function"; 89 else if (MD->getMacroInfo()->isFunctionLike()) 90 Message = "function-like macro '%0' used; consider a 'constexpr' template " 91 "function"; 92 93 diag(MD->getLocation(), Message) << MacroName; 94 } 95 96 void MacroUsageCheck::warnNaming(const MacroDirective *MD, 97 StringRef MacroName) { 98 diag(MD->getLocation(), "macro definition does not define the macro name " 99 "'%0' using all uppercase characters") 100 << MacroName; 101 } 102 103 } // namespace cppcoreguidelines 104 } // namespace tidy 105 } // namespace clang 106