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
isCapsOnly(StringRef Name)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:
MacroUsageCallbacks(MacroUsageCheck * Check,const SourceManager & SM,StringRef RegExpStr,bool CapsOnly,bool IgnoreCommandLine)34 MacroUsageCallbacks(MacroUsageCheck *Check, const SourceManager &SM,
35 StringRef RegExpStr, bool CapsOnly, bool IgnoreCommandLine)
36 : Check(Check), SM(SM), RegExp(RegExpStr), CheckCapsOnly(CapsOnly),
37 IgnoreCommandLineMacros(IgnoreCommandLine) {}
MacroDefined(const Token & MacroNameTok,const MacroDirective * MD)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 && !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 const llvm::Regex RegExp;
61 bool CheckCapsOnly;
62 bool IgnoreCommandLineMacros;
63 };
64 } // namespace
65
storeOptions(ClangTidyOptions::OptionMap & Opts)66 void MacroUsageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
67 Options.store(Opts, "AllowedRegexp", AllowedRegexp);
68 Options.store(Opts, "CheckCapsOnly", CheckCapsOnly);
69 Options.store(Opts, "IgnoreCommandLineMacros", IgnoreCommandLineMacros);
70 }
71
registerPPCallbacks(const SourceManager & SM,Preprocessor * PP,Preprocessor * ModuleExpanderPP)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
warnMacro(const MacroDirective * MD,StringRef MacroName)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
warnNaming(const MacroDirective * MD,StringRef MacroName)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
107