xref: /llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp (revision c367ba1923335a90e760923dd8b93164ff9af22f)
1 //===--- MacroUsageCheck.cpp - clang-tidy----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "MacroUsageCheck.h"
11 #include "clang/Frontend/CompilerInstance.h"
12 #include "clang/Lex/PPCallbacks.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 RegExp, bool CapsOnly, bool IgnoreCommandLine)
36       : Check(Check), SM(SM), RegExp(RegExp), CheckCapsOnly(CapsOnly),
37         IgnoreCommandLineMacros(IgnoreCommandLine) {}
38   void MacroDefined(const Token &MacroNameTok,
39                     const MacroDirective *MD) override {
40     if (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(CompilerInstance &Compiler) {
72   if (!getLangOpts().CPlusPlus11)
73     return;
74 
75   Compiler.getPreprocessor().addPPCallbacks(
76       llvm::make_unique<MacroUsageCallbacks>(this, Compiler.getSourceManager(),
77                                              AllowedRegexp, CheckCapsOnly,
78                                              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