[clang-tools-extra] [clang-tidy] Add new check: `readability-use-concise-preprocessor-directives` (PR #146830)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 6 15:20:00 PDT 2025


================
@@ -0,0 +1,111 @@
+//===--- 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"
+
+#include <array>
+
+namespace clang::tidy::readability {
+
+namespace {
+
+class IfPreprocessorCallbacks final : public PPCallbacks {
+public:
+  IfPreprocessorCallbacks(ClangTidyCheck &Check, const 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"});
+    }
----------------
vbvictor wrote:

```suggestion
    if (PP.getLangOpts().C23 || PP.getLangOpts().CPlusPlus23)
      impl(Loc, ConditionRange, {"elifdef", "elifndef"});
```
Probable enabling https://clang.llvm.org/docs/ClangFormatStyleOptions.html#removebracesllvm option will catch it

https://github.com/llvm/llvm-project/pull/146830


More information about the cfe-commits mailing list