clang-tools  15.0.0git
MacroUsageCheck.cpp
Go to the documentation of this file.
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 
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
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::doc::MD
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
clang::tidy::cppcoreguidelines::MacroUsageCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: MacroUsageCheck.cpp:74
clang::tidy::bugprone::Message
static const char Message[]
Definition: ReservedIdentifierCheck.cpp:31
clang::tidy::cppcoreguidelines::MacroUsageCheck::warnMacro
void warnMacro(const MacroDirective *MD, StringRef MacroName)
Definition: MacroUsageCheck.cpp:81
clang::tidy::cppcoreguidelines::isLiteral
static bool isLiteral(const Expr *E)
Definition: PreferMemberInitializerCheck.cpp:37
clang::tidy::cppcoreguidelines::MacroUsageCheck::warnNaming
void warnNaming(const MacroDirective *MD, StringRef MacroName)
Definition: MacroUsageCheck.cpp:102
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:415
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::tidy::cppcoreguidelines::isCapsOnly
static bool isCapsOnly(StringRef Name)
Definition: MacroUsageCheck.cpp:23
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:121
clang::tidy::cppcoreguidelines::MacroUsageCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: MacroUsageCheck.cpp:68
C
const Criteria C
Definition: FunctionCognitiveComplexityCheck.cpp:93
PPCallbacks
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidyCheck.cpp:129
MacroUsageCheck.h