CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
118 lines
4.3 KiB
C++
118 lines
4.3 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "MacroUsageCheck.h"
|
|
#include "clang/Basic/TokenKinds.h"
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
#include "clang/Lex/Preprocessor.h"
|
|
#include "llvm/ADT/STLExtras.h"
|
|
#include "llvm/Support/Regex.h"
|
|
#include <cctype>
|
|
#include <functional>
|
|
|
|
namespace clang::tidy::cppcoreguidelines {
|
|
|
|
static bool isCapsOnly(StringRef Name) {
|
|
return llvm::all_of(Name, [](const char C) {
|
|
return std::isupper(C) || std::isdigit(C) || C == '_';
|
|
});
|
|
}
|
|
|
|
namespace {
|
|
|
|
class MacroUsageCallbacks : public PPCallbacks {
|
|
public:
|
|
MacroUsageCallbacks(MacroUsageCheck *Check, const SourceManager &SM,
|
|
StringRef RegExpStr, bool CapsOnly,
|
|
bool IgnoreCommandLine)
|
|
: Check(Check), SM(SM), RegExp(RegExpStr), CheckCapsOnly(CapsOnly),
|
|
IgnoreCommandLineMacros(IgnoreCommandLine) {}
|
|
void MacroDefined(const Token &MacroNameTok,
|
|
const MacroDirective *MD) override {
|
|
if (SM.isWrittenInBuiltinFile(MD->getLocation()) ||
|
|
MD->getMacroInfo()->isUsedForHeaderGuard() ||
|
|
MD->getMacroInfo()->tokens_empty() ||
|
|
llvm::any_of(MD->getMacroInfo()->tokens(), [](const Token &T) {
|
|
return T.isOneOf(tok::TokenKind::hash, tok::TokenKind::hashhash);
|
|
}))
|
|
return;
|
|
|
|
if (IgnoreCommandLineMacros &&
|
|
SM.isWrittenInCommandLineFile(MD->getLocation()))
|
|
return;
|
|
|
|
const StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();
|
|
if (MacroName == "__GCC_HAVE_DWARF2_CFI_ASM")
|
|
return;
|
|
if (!CheckCapsOnly && !RegExp.match(MacroName))
|
|
Check->warnMacro(MD, MacroName);
|
|
|
|
if (CheckCapsOnly && !isCapsOnly(MacroName))
|
|
Check->warnNaming(MD, MacroName);
|
|
}
|
|
|
|
private:
|
|
MacroUsageCheck *Check;
|
|
const SourceManager &SM;
|
|
const llvm::Regex RegExp;
|
|
bool CheckCapsOnly;
|
|
bool IgnoreCommandLineMacros;
|
|
};
|
|
} // namespace
|
|
|
|
void MacroUsageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
Options.store(Opts, "AllowedRegexp", AllowedRegexp);
|
|
Options.store(Opts, "CheckCapsOnly", CheckCapsOnly);
|
|
Options.store(Opts, "IgnoreCommandLineMacros", IgnoreCommandLineMacros);
|
|
}
|
|
|
|
void MacroUsageCheck::registerPPCallbacks(const SourceManager &SM,
|
|
Preprocessor *PP,
|
|
Preprocessor *ModuleExpanderPP) {
|
|
PP->addPPCallbacks(std::make_unique<MacroUsageCallbacks>(
|
|
this, SM, AllowedRegexp, CheckCapsOnly, IgnoreCommandLineMacros));
|
|
}
|
|
|
|
void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
|
|
const MacroInfo *Info = MD->getMacroInfo();
|
|
StringRef Message;
|
|
bool MacroBodyExpressionLike = true;
|
|
if (Info->getNumTokens() > 0) {
|
|
const Token &Tok = Info->getReplacementToken(0);
|
|
// Now notice that keywords like `__attribute` cannot be a leading
|
|
// token in an expression.
|
|
MacroBodyExpressionLike = !Tok.is(tok::kw___attribute);
|
|
}
|
|
|
|
if (llvm::all_of(Info->tokens(), std::mem_fn(&Token::isLiteral)))
|
|
Message = "macro '%0' used to declare a constant; consider using a "
|
|
"'constexpr' constant";
|
|
// A variadic macro is function-like at the same time. Therefore variadic
|
|
// macros are checked first and will be excluded for the function-like
|
|
// diagnostic.
|
|
else if (Info->isVariadic() && MacroBodyExpressionLike)
|
|
Message = "variadic macro '%0' used; consider using a 'constexpr' "
|
|
"variadic template function";
|
|
else if (Info->isFunctionLike() && MacroBodyExpressionLike)
|
|
Message = "function-like macro '%0' used; consider a 'constexpr' template "
|
|
"function";
|
|
|
|
if (!Message.empty())
|
|
diag(MD->getLocation(), Message) << MacroName;
|
|
}
|
|
|
|
void MacroUsageCheck::warnNaming(const MacroDirective *MD,
|
|
StringRef MacroName) {
|
|
diag(MD->getLocation(), "macro definition does not define the macro name "
|
|
"'%0' using all uppercase characters")
|
|
<< MacroName;
|
|
}
|
|
|
|
} // namespace clang::tidy::cppcoreguidelines
|