hokein created this revision. hokein added a reviewer: sammccall. Herald added a project: All. hokein requested review of this revision. Herald added a project: clang.
Fixes https://github.com/llvm/llvm-project/issues/60722. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D144054 Files: clang/lib/Lex/TokenLexer.cpp clang/test/Lexer/update_consecutive_macro_crash.cpp Index: clang/test/Lexer/update_consecutive_macro_crash.cpp =================================================================== --- /dev/null +++ clang/test/Lexer/update_consecutive_macro_crash.cpp @@ -0,0 +1,11 @@ +// RUN: %clang -cc1 -fsyntax-only -verify %s 2>&1 + +// https://github.com/llvm/llvm-project/issues/60722 +#define X(val2) Y(val2++) // expected-note {{macro 'X' defined here}} +#define Y(expression) expression ; + +void foo() { + X(int{,}); // expected-error {{too many arguments provided to function-like macro invocation}} \ + expected-error {{expected expression}} \ + expected-note {{parentheses are required around macro argument containing braced initializer list}} +} Index: clang/lib/Lex/TokenLexer.cpp =================================================================== --- clang/lib/Lex/TokenLexer.cpp +++ clang/lib/Lex/TokenLexer.cpp @@ -1020,8 +1020,17 @@ SourceLocation Limit = SM.getComposedLoc(BeginFID, SM.getFileIDSize(BeginFID)); Partition = All.take_while([&](const Token &T) { - return T.getLocation() >= BeginLoc && T.getLocation() < Limit && - NearLast(T.getLocation()); + // NOTE: the Limit is included! In general, all token locations + // should be within [BeginLoc, Limit) range. However, the clang + // lexer might inject extra tokens for error-recovery, and these extra + // tokens are not taken account in the expansion FileID size! + // + // Including Limit will not mischeck for across-file-id tokens + // because SourceManager allocates FileSize + 1 for each SLocEntry. + // + // See https://github.com/llvm/llvm-project/issues/60722. + return T.getLocation() >= BeginLoc && T.getLocation() <= Limit + && NearLast(T.getLocation()); }); } assert(!Partition.empty());
Index: clang/test/Lexer/update_consecutive_macro_crash.cpp =================================================================== --- /dev/null +++ clang/test/Lexer/update_consecutive_macro_crash.cpp @@ -0,0 +1,11 @@ +// RUN: %clang -cc1 -fsyntax-only -verify %s 2>&1 + +// https://github.com/llvm/llvm-project/issues/60722 +#define X(val2) Y(val2++) // expected-note {{macro 'X' defined here}} +#define Y(expression) expression ; + +void foo() { + X(int{,}); // expected-error {{too many arguments provided to function-like macro invocation}} \ + expected-error {{expected expression}} \ + expected-note {{parentheses are required around macro argument containing braced initializer list}} +} Index: clang/lib/Lex/TokenLexer.cpp =================================================================== --- clang/lib/Lex/TokenLexer.cpp +++ clang/lib/Lex/TokenLexer.cpp @@ -1020,8 +1020,17 @@ SourceLocation Limit = SM.getComposedLoc(BeginFID, SM.getFileIDSize(BeginFID)); Partition = All.take_while([&](const Token &T) { - return T.getLocation() >= BeginLoc && T.getLocation() < Limit && - NearLast(T.getLocation()); + // NOTE: the Limit is included! In general, all token locations + // should be within [BeginLoc, Limit) range. However, the clang + // lexer might inject extra tokens for error-recovery, and these extra + // tokens are not taken account in the expansion FileID size! + // + // Including Limit will not mischeck for across-file-id tokens + // because SourceManager allocates FileSize + 1 for each SLocEntry. + // + // See https://github.com/llvm/llvm-project/issues/60722. + return T.getLocation() >= BeginLoc && T.getLocation() <= Limit + && NearLast(T.getLocation()); }); } assert(!Partition.empty());
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits