[PATCH] D144054: [Lex] Fix a crash in updateConsecutiveMacroArgTokens.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 14 15:41:03 PST 2023


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());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144054.497463.patch
Type: text/x-patch
Size: 1853 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230214/d15c9097/attachment-0001.bin>


More information about the cfe-commits mailing list