xref: /llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp (revision d83ecd77cc0f16cb5fbabe03d37829893ac8b56d)
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 #include <functional>
18 
19 namespace clang {
20 namespace tidy {
21 namespace cppcoreguidelines {
22 
23 static bool isCapsOnly(StringRef Name) {
24   return llvm::all_of(Name, [](const char C) {
25     return std::isupper(C) || std::isdigit(C) || C == '_';
26   });
27 }
28 
29 namespace {
30 
31 class MacroUsageCallbacks : public PPCallbacks {
32 public:
33   MacroUsageCallbacks(MacroUsageCheck *Check, const SourceManager &SM,
34                       StringRef RegExpStr, bool CapsOnly,
35                       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   const MacroInfo *Info = MD->getMacroInfo();
83   StringRef Message;
84 
85   if (llvm::all_of(Info->tokens(), std::mem_fn(&Token::isLiteral)))
86     Message = "macro '%0' used to declare a constant; consider using a "
87               "'constexpr' constant";
88   // A variadic macro is function-like at the same time. Therefore variadic
89   // macros are checked first and will be excluded for the function-like
90   // diagnostic.
91   else if (Info->isVariadic())
92     Message = "variadic macro '%0' used; consider using a 'constexpr' "
93               "variadic template function";
94   else if (Info->isFunctionLike())
95     Message = "function-like macro '%0' used; consider a 'constexpr' template "
96               "function";
97 
98   if (!Message.empty())
99     diag(MD->getLocation(), Message) << MacroName;
100 }
101 
102 void MacroUsageCheck::warnNaming(const MacroDirective *MD,
103                                  StringRef MacroName) {
104   diag(MD->getLocation(), "macro definition does not define the macro name "
105                           "'%0' using all uppercase characters")
106       << MacroName;
107 }
108 
109 } // namespace cppcoreguidelines
110 } // namespace tidy
111 } // namespace clang
112