[clang-tools-extra] [clang-tidy] Add new check: `modernize-use-concise-preprocessor-directives` (PR #146830)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 3 08:07:33 PDT 2025
================
@@ -0,0 +1,105 @@
+//===--- UseConcisePreprocessorDirectivesCheck.cpp - clang-tidy -----------===//
+//
+// 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 "UseConcisePreprocessorDirectivesCheck.h"
+#include "clang/Basic/TokenKinds.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/Lex/PPCallbacks.h"
+#include "clang/Lex/Preprocessor.h"
+
+namespace clang::tidy::modernize {
+
+namespace {
+
+class IfPreprocessorCallbacks final : public PPCallbacks {
+public:
+ IfPreprocessorCallbacks(ClangTidyCheck &Check, Preprocessor &PP)
+ : Check(Check), PP(PP) {}
+
+ void If(SourceLocation Loc, SourceRange ConditionRange,
+ ConditionValueKind) override {
+ impl(Loc, ConditionRange, {"ifdef", "ifndef"});
+ }
+
+ void Elif(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind,
+ SourceLocation) override {
+ if (PP.getLangOpts().C23 || PP.getLangOpts().CPlusPlus23) {
+ impl(Loc, ConditionRange, {"elifdef", "elifndef"});
+ }
+ }
+
+private:
+ void impl(SourceLocation DirectiveLoc, SourceRange ConditionRange,
+ const llvm::StringLiteral (&Replacements)[2]) {
+ StringRef Condition =
----------------
EugeneZelenko wrote:
Should be `const`. Same for some other variables.
https://github.com/llvm/llvm-project/pull/146830
More information about the cfe-commits
mailing list