Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/lib/Lex/PPExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions clang/test/Index/single-file-parse-undefined-function-like-macro.c
Original file line number Diff line number Diff line change
@@ -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
Loading