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

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 22 01:15:36 PST 2023


hokein updated this revision to Diff 499405.
hokein marked 2 inline comments as done.
hokein added a comment.

address comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144054/new/

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,17 @@
+// RUN: %clang -cc1 -fsyntax-only -verify %s 2>&1
+
+#define X(val2) Y(val2++) // expected-note {{macro 'X' defined here}}
+#define Y(expression) expression ;
+
+void foo() {
+  // https://github.com/llvm/llvm-project/issues/60722:
+  //
+  // - Due to to the error recovery, the lexer inserts a pair of () around the
+  //   macro argument int{,}, so we will see [(, int, {, ,, }, )] tokens.
+  // - however, the size of file id for the macro argument only takes account
+  //   the written tokens  int{,} , and the extra inserted ) token points to the
+  //    Limit source location which triggered an empty Partition violation.
+  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
@@ -1019,8 +1019,16 @@
     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! The lexer recovery only ever inserts a
+      // single token past the end of the FileID, specifically the ) when a
+      // macro-arg containing a comma should be guarded by parentheses.
+      //
+      // It is safe to include the Limit here 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.499405.patch
Type: text/x-patch
Size: 2171 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230222/7da5bf24/attachment.bin>


More information about the cfe-commits mailing list