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 (MD->getMacroInfo()->isUsedForHeaderGuard() ||
40         MD->getMacroInfo()->getNumTokens() == 0)
41       return;
42 
43     if (IgnoreCommandLineMacros &&
44         SM.isWrittenInCommandLineFile(MD->getLocation()))
45       return;
46 
47     StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();
48     if (!CheckCapsOnly && !llvm::Regex(RegExp).match(MacroName))
49       Check->warnMacro(MD, MacroName);
50 
51     if (CheckCapsOnly && !isCapsOnly(MacroName))
52       Check->warnNaming(MD, MacroName);
53   }
54 
55 private:
56   MacroUsageCheck *Check;
57   const SourceManager &SM;
58   StringRef RegExp;
59   bool CheckCapsOnly;
60   bool IgnoreCommandLineMacros;
61 };
62 } // namespace
63 
64 void MacroUsageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
65   Options.store(Opts, "AllowedRegexp", AllowedRegexp);
66   Options.store(Opts, "CheckCapsOnly", CheckCapsOnly);
67   Options.store(Opts, "IgnoreCommandLineMacros", IgnoreCommandLineMacros);
68 }
69 
70 void MacroUsageCheck::registerPPCallbacks(CompilerInstance &Compiler) {
71   if (!getLangOpts().CPlusPlus11)
72     return;
73 
74   Compiler.getPreprocessor().addPPCallbacks(
75       llvm::make_unique<MacroUsageCallbacks>(this, Compiler.getSourceManager(),
76                                              AllowedRegexp, CheckCapsOnly,
77                                              IgnoreCommandLineMacros));
78 }
79 
80 void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
81   StringRef Message =
82       "macro '%0' used to declare a constant; consider using a 'constexpr' "
83       "constant";
84 
85   /// A variadic macro is function-like at the same time. Therefore variadic
86   /// macros are checked first and will be excluded for the function-like
87   /// diagnostic.
88   if (MD->getMacroInfo()->isVariadic())
89     Message = "variadic macro '%0' used; consider using a 'constexpr' "
90               "variadic template function";
91   else if (MD->getMacroInfo()->isFunctionLike())
92     Message = "function-like macro '%0' used; consider a 'constexpr' template "
93               "function";
94 
95   diag(MD->getLocation(), Message) << MacroName;
96 }
97 
98 void MacroUsageCheck::warnNaming(const MacroDirective *MD,
99                                  StringRef MacroName) {
100   diag(MD->getLocation(), "macro definition does not define the macro name "
101                           "'%0' using all uppercase characters")
102       << MacroName;
103 }
104 
105 } // namespace cppcoreguidelines
106 } // namespace tidy
107 } // namespace clang
108