clang-tools  11.0.0
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 
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 (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 (!CheckCapsOnly && !llvm::Regex(RegExp).match(MacroName))
51  Check->warnMacro(MD, MacroName);
52 
53  if (CheckCapsOnly && !isCapsOnly(MacroName))
54  Check->warnNaming(MD, MacroName);
55  }
56 
57 private:
58  MacroUsageCheck *Check;
59  const SourceManager &SM;
60  StringRef RegExp;
61  bool CheckCapsOnly;
62  bool IgnoreCommandLineMacros;
63 };
64 } // namespace
65 
67  Options.store(Opts, "AllowedRegexp", AllowedRegexp);
68  Options.store(Opts, "CheckCapsOnly", CheckCapsOnly);
69  Options.store(Opts, "IgnoreCommandLineMacros", IgnoreCommandLineMacros);
70 }
71 
72 void MacroUsageCheck::registerPPCallbacks(const SourceManager &SM,
73  Preprocessor *PP,
74  Preprocessor *ModuleExpanderPP) {
75  PP->addPPCallbacks(std::make_unique<MacroUsageCallbacks>(
76  this, SM, AllowedRegexp, CheckCapsOnly, IgnoreCommandLineMacros));
77 }
78 
79 void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
80  StringRef Message =
81  "macro '%0' used to declare a constant; consider using a 'constexpr' "
82  "constant";
83 
84  /// A variadic macro is function-like at the same time. Therefore variadic
85  /// macros are checked first and will be excluded for the function-like
86  /// diagnostic.
87  if (MD->getMacroInfo()->isVariadic())
88  Message = "variadic macro '%0' used; consider using a 'constexpr' "
89  "variadic template function";
90  else if (MD->getMacroInfo()->isFunctionLike())
91  Message = "function-like macro '%0' used; consider a 'constexpr' template "
92  "function";
93 
94  diag(MD->getLocation(), Message) << MacroName;
95 }
96 
97 void MacroUsageCheck::warnNaming(const MacroDirective *MD,
98  StringRef MacroName) {
99  diag(MD->getLocation(), "macro definition does not define the macro name "
100  "'%0' using all uppercase characters")
101  << MacroName;
102 }
103 
104 } // namespace cppcoreguidelines
105 } // namespace tidy
106 } // namespace clang
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:72
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:79
clang::tidy::cppcoreguidelines::MacroUsageCheck::warnNaming
void warnNaming(const MacroDirective *MD, StringRef MacroName)
Definition: MacroUsageCheck.cpp:97
clang::clangd::match
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
Definition: TestIndex.cpp:94
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:471
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
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:55
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
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:66
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:152
MacroUsageCheck.h
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111