[clang-tools-extra] [clang-tidy] Add bugprone-macro-condition check (PR #210768)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 3 00:03:47 PDT 2026
================
@@ -0,0 +1,357 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "MacroConditionCheck.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/Lex/MacroInfo.h"
+#include "clang/Lex/PPCallbacks.h"
+#include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringSet.h"
+#include <memory>
+#include <string>
+#include <utility>
+
+namespace clang::tidy::bugprone {
+
+namespace {
+class MacroConditionCallbacks : public PPCallbacks {
+public:
+ MacroConditionCallbacks(MacroConditionCheck *Check, const SourceManager &SM,
+ Preprocessor &PP)
+ : Check(Check), SM(SM), PP(PP) {}
+
+ void If(SourceLocation Loc, SourceRange ConditionRange,
+ ConditionValueKind ConditionValue) override;
+ void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroDefinition &MD) override;
+ void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroDefinition &MD) override;
+ void Elif(SourceLocation Loc, SourceRange ConditionRange,
+ ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
+ void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroDefinition &MD) override;
+ void Elifdef(SourceLocation Loc, SourceRange ConditionRange,
+ SourceLocation IfLoc) override;
+ void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroDefinition &MD) override;
+ void Elifndef(SourceLocation Loc, SourceRange ConditionRange,
+ SourceLocation IfLoc) override;
+ void Else(SourceLocation Loc, SourceLocation IfLoc) override;
+ void Endif(SourceLocation Loc, SourceLocation IfLoc) override;
+
+private:
+ struct MacroReference {
+ std::string Name;
+ SourceLocation Loc;
+ };
+
+ struct ConditionReferences {
+ SmallVector<MacroReference, 2> Definition;
+ SmallVector<MacroReference, 2> Value;
+ };
+
+ struct DefinitionCheck {
+ std::string Name;
+ SourceLocation DefinitionLoc;
+ SourceLocation CheckLoc;
+ bool ValueTested = false;
+ };
+
+ struct ConditionalBranch {
+ SmallVector<DefinitionCheck, 2> Checks;
+ llvm::StringSet<> DefinitionTests;
+ };
+
+ ConditionReferences referencesInCondition(SourceRange ConditionRange) const;
+ MacroReference referenceFromRange(SourceRange Range,
+ SourceLocation Loc) const;
+ bool isIgnoredIdentifier(StringRef Name) const;
+ void startCondition(const ConditionReferences &References);
+ void startDefinitionCondition(StringRef Name, SourceLocation Loc);
+ void nextBranch(const ConditionReferences &References = {});
+ void processReferences(const ConditionReferences &References);
+ void finishBranch(ConditionalBranch &Branch);
+ void checkDefinitionReference(const MacroReference &Reference,
+ ConditionalBranch &Branch);
+ void checkValueReference(const MacroReference &Reference);
+ bool isDefinitionTestActive(StringRef Name) const;
+
+ SmallVector<ConditionalBranch, 8> Conditions;
+ llvm::DenseSet<unsigned> DiagnosedDefinitions;
+ MacroConditionCheck *Check;
+ const SourceManager &SM;
+ Preprocessor &PP;
+};
+
+} // namespace
+
+static StringRef getTokenName(const Token &Tok) {
+ if (Tok.is(tok::raw_identifier))
+ return Tok.getRawIdentifier();
+ if (const IdentifierInfo *Info = Tok.getIdentifierInfo())
+ return Info->getName();
+ return {};
+}
+
+MacroConditionCallbacks::ConditionReferences
+MacroConditionCallbacks::referencesInCondition(
+ SourceRange ConditionRange) const {
+ ConditionReferences References;
+ const SourceLocation BeginLoc = SM.getExpansionLoc(ConditionRange.getBegin());
+ if (BeginLoc.isInvalid())
+ return References;
+
+ const std::pair<FileID, unsigned> Decomposed = SM.getDecomposedLoc(BeginLoc);
+ bool Invalid = false;
+ StringRef Buffer = SM.getBufferData(Decomposed.first, &Invalid);
+ if (Invalid || Decomposed.second >= Buffer.size())
+ return References;
+
+ size_t End = Decomposed.second;
+ while (End < Buffer.size()) {
----------------
zeyi2 wrote:
Could we use `Lexer::getSourceText(CharSourceRange::getTokenRange(ConditionRange), ...)` here?
AFAIK [`readability-use-concise-preprocessor-directives`](https://github.com/llvm/llvm-project/blob/4f607e3cd911ccc3d1aab73129aacea97d864663/clang-tools-extra/clang-tidy/readability/UseConcisePreprocessorDirectivesCheck.cpp#L38-L46) handles the same `PPCallbacks` range this way.
https://github.com/llvm/llvm-project/pull/210768
More information about the cfe-commits
mailing list