[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clang-tidy / cppcoreguidelines / MacroUsageCheck.cpp
blob0cd4bf6fdfd8775cbd73eb46323e37b1ed1779c6
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 //===----------------------------------------------------------------------===//
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>
19 namespace clang::tidy::cppcoreguidelines {
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 });
27 namespace {
29 class MacroUsageCallbacks : public PPCallbacks {
30 public:
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) {}
36 void MacroDefined(const Token &MacroNameTok,
37 const MacroDirective *MD) override {
38 if (SM.isWrittenInBuiltinFile(MD->getLocation()) ||
39 MD->getMacroInfo()->isUsedForHeaderGuard() ||
40 MD->getMacroInfo()->getNumTokens() == 0)
41 return;
43 if (IgnoreCommandLineMacros &&
44 SM.isWrittenInCommandLineFile(MD->getLocation()))
45 return;
47 StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();
48 if (MacroName == "__GCC_HAVE_DWARF2_CFI_ASM")
49 return;
50 if (!CheckCapsOnly && !RegExp.match(MacroName))
51 Check->warnMacro(MD, MacroName);
53 if (CheckCapsOnly && !isCapsOnly(MacroName))
54 Check->warnNaming(MD, MacroName);
57 private:
58 MacroUsageCheck *Check;
59 const SourceManager &SM;
60 const llvm::Regex RegExp;
61 bool CheckCapsOnly;
62 bool IgnoreCommandLineMacros;
64 } // namespace
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);
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));
79 void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
80 const MacroInfo *Info = MD->getMacroInfo();
81 StringRef Message;
83 if (llvm::all_of(Info->tokens(), std::mem_fn(&Token::isLiteral)))
84 Message = "macro '%0' used to declare a constant; consider using a "
85 "'constexpr' constant";
86 // A variadic macro is function-like at the same time. Therefore variadic
87 // macros are checked first and will be excluded for the function-like
88 // diagnostic.
89 else if (Info->isVariadic())
90 Message = "variadic macro '%0' used; consider using a 'constexpr' "
91 "variadic template function";
92 else if (Info->isFunctionLike())
93 Message = "function-like macro '%0' used; consider a 'constexpr' template "
94 "function";
96 if (!Message.empty())
97 diag(MD->getLocation(), Message) << MacroName;
100 void MacroUsageCheck::warnNaming(const MacroDirective *MD,
101 StringRef MacroName) {
102 diag(MD->getLocation(), "macro definition does not define the macro name "
103 "'%0' using all uppercase characters")
104 << MacroName;
107 } // namespace clang::tidy::cppcoreguidelines