xref: /llvm-project/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp (revision b06da39cae0f7e2c6b8bc0bb03b734f9715c0bf3)
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/Basic/TokenKinds.h"
11 #include "clang/Frontend/CompilerInstance.h"
12 #include "clang/Lex/PPCallbacks.h"
13 #include "clang/Lex/Preprocessor.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/Support/Regex.h"
16 #include <cctype>
17 #include <functional>
18 
19 namespace clang::tidy::cppcoreguidelines {
20 
isCapsOnly(StringRef Name)21 static bool isCapsOnly(StringRef Name) {
22   return llvm::all_of(Name, [](const char C) {
23     return std::isupper(C) || std::isdigit(C) || C == '_';
24   });
25 }
26 
27 namespace {
28 
29 class MacroUsageCallbacks : public PPCallbacks {
30 public:
MacroUsageCallbacks(MacroUsageCheck * Check,const SourceManager & SM,StringRef RegExpStr,bool CapsOnly,bool IgnoreCommandLine)31   MacroUsageCallbacks(MacroUsageCheck *Check, const SourceManager &SM,
32                       StringRef RegExpStr, bool CapsOnly,
33                       bool IgnoreCommandLine)
34       : Check(Check), SM(SM), RegExp(RegExpStr), CheckCapsOnly(CapsOnly),
35         IgnoreCommandLineMacros(IgnoreCommandLine) {}
MacroDefined(const Token & MacroNameTok,const MacroDirective * MD)36   void MacroDefined(const Token &MacroNameTok,
37                     const MacroDirective *MD) override {
38     if (SM.isWrittenInBuiltinFile(MD->getLocation()) ||
39         MD->getMacroInfo()->isUsedForHeaderGuard() ||
40         MD->getMacroInfo()->tokens_empty() ||
41         llvm::any_of(MD->getMacroInfo()->tokens(), [](const Token &T) {
42           return T.isOneOf(tok::TokenKind::hash, tok::TokenKind::hashhash);
43         }))
44       return;
45 
46     if (IgnoreCommandLineMacros &&
47         SM.isWrittenInCommandLineFile(MD->getLocation()))
48       return;
49 
50     StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();
51     if (MacroName == "__GCC_HAVE_DWARF2_CFI_ASM")
52       return;
53     if (!CheckCapsOnly && !RegExp.match(MacroName))
54       Check->warnMacro(MD, MacroName);
55 
56     if (CheckCapsOnly && !isCapsOnly(MacroName))
57       Check->warnNaming(MD, MacroName);
58   }
59 
60 private:
61   MacroUsageCheck *Check;
62   const SourceManager &SM;
63   const llvm::Regex RegExp;
64   bool CheckCapsOnly;
65   bool IgnoreCommandLineMacros;
66 };
67 } // namespace
68 
storeOptions(ClangTidyOptions::OptionMap & Opts)69 void MacroUsageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
70   Options.store(Opts, "AllowedRegexp", AllowedRegexp);
71   Options.store(Opts, "CheckCapsOnly", CheckCapsOnly);
72   Options.store(Opts, "IgnoreCommandLineMacros", IgnoreCommandLineMacros);
73 }
74 
registerPPCallbacks(const SourceManager & SM,Preprocessor * PP,Preprocessor * ModuleExpanderPP)75 void MacroUsageCheck::registerPPCallbacks(const SourceManager &SM,
76                                           Preprocessor *PP,
77                                           Preprocessor *ModuleExpanderPP) {
78   PP->addPPCallbacks(std::make_unique<MacroUsageCallbacks>(
79       this, SM, AllowedRegexp, CheckCapsOnly, IgnoreCommandLineMacros));
80 }
81 
warnMacro(const MacroDirective * MD,StringRef MacroName)82 void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
83   const MacroInfo *Info = MD->getMacroInfo();
84   StringRef Message;
85 
86   if (llvm::all_of(Info->tokens(), std::mem_fn(&Token::isLiteral)))
87     Message = "macro '%0' used to declare a constant; consider using a "
88               "'constexpr' constant";
89   // A variadic macro is function-like at the same time. Therefore variadic
90   // macros are checked first and will be excluded for the function-like
91   // diagnostic.
92   else if (Info->isVariadic())
93     Message = "variadic macro '%0' used; consider using a 'constexpr' "
94               "variadic template function";
95   else if (Info->isFunctionLike())
96     Message = "function-like macro '%0' used; consider a 'constexpr' template "
97               "function";
98 
99   if (!Message.empty())
100     diag(MD->getLocation(), Message) << MacroName;
101 }
102 
warnNaming(const MacroDirective * MD,StringRef MacroName)103 void MacroUsageCheck::warnNaming(const MacroDirective *MD,
104                                  StringRef MacroName) {
105   diag(MD->getLocation(), "macro definition does not define the macro name "
106                           "'%0' using all uppercase characters")
107       << MacroName;
108 }
109 
110 } // namespace clang::tidy::cppcoreguidelines
111