[clang] fe2eefc - [clang][index] Handle undefined function-like macros in single file parse mode (#135054)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 9 13:00:21 PDT 2025
Author: Jan Svoboda
Date: 2025-04-09T13:00:17-07:00
New Revision: fe2eefc4718f57e1753f7bd51c158fc03d70b34f
URL: https://github.com/llvm/llvm-project/commit/fe2eefc4718f57e1753f7bd51c158fc03d70b34f
DIFF: https://github.com/llvm/llvm-project/commit/fe2eefc4718f57e1753f7bd51c158fc03d70b34f.diff
LOG: [clang][index] Handle undefined function-like macros in single file parse mode (#135054)
The single file parse mode is supposed to enter both branches of an
`#if` directive whenever the condition contains undefined identifiers.
This patch adds support for undefined function-like macros, where we
would previously emit an error that doesn't make sense from end-user
perspective.
(I discovered this while working on a very similar feature that parses
single module only and doesn't enter either `#if` branch when the
condition contains undefined identifiers.)
Added:
clang/test/Index/single-file-parse-undefined-function-like-macro.c
Modified:
clang/lib/Lex/PPExpressions.cpp
Removed:
################################################################################
diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp
index 48835121b40e9..a202af774256a 100644
--- a/clang/lib/Lex/PPExpressions.cpp
+++ b/clang/lib/Lex/PPExpressions.cpp
@@ -26,6 +26,7 @@
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Lex/Token.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/STLExtras.h"
@@ -592,6 +593,15 @@ static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
Token &PeekTok, bool ValueLive,
bool &IncludedUndefinedIds,
Preprocessor &PP) {
+ if (PP.getPreprocessorOpts().SingleFileParseMode && IncludedUndefinedIds) {
+ // The single-file parse mode behavior kicks in as soon as single identifier
+ // is undefined. If we've already seen one, there's no point in continuing
+ // with the rest of the expression. Besides saving work, this also prevents
+ // calling undefined function-like macros.
+ PP.DiscardUntilEndOfDirective(PeekTok);
+ return true;
+ }
+
unsigned PeekPrec = getPrecedence(PeekTok.getKind());
// If this token isn't valid, report the error.
if (PeekPrec == ~0U) {
diff --git a/clang/test/Index/single-file-parse-undefined-function-like-macro.c b/clang/test/Index/single-file-parse-undefined-function-like-macro.c
new file mode 100644
index 0000000000000..4d2da1382fcd9
--- /dev/null
+++ b/clang/test/Index/single-file-parse-undefined-function-like-macro.c
@@ -0,0 +1,15 @@
+// RUN: split-file %s %t
+// RUN: c-index-test -single-file-parse %t/tu.c 2>&1 | FileCheck %t/tu.c
+
+//--- header.h
+#define FUNCTION_LIKE_MACRO() 1
+//--- tu.c
+#include "header.h"
+// CHECK-NOT: tu.c:[[@LINE+1]]:5: error: function-like macro 'FUNCTION_LIKE_MACRO' is not defined
+#if FUNCTION_LIKE_MACRO()
+// CHECK: tu.c:[[@LINE+1]]:5: FunctionDecl=then_fn
+int then_fn();
+#else
+// CHECK: tu.c:[[@LINE+1]]:5: FunctionDecl=else_fn
+int else_fn();
+#endif
More information about the cfe-commits
mailing list