[PATCH] D24839: [MC] Prevent out of order HashDirective lexing in AsmLexer.

Nirav Dave via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 22 12:12:18 PDT 2016


niravd created this revision.
niravd added reviewers: rnk, loladiro.
niravd added a subscriber: llvm-commits.

To lex hash directives we peek ahead to find component tokens, create a
unified token, and unlex the peeked tokens so the parser does not need
to parse the tokens then. Make sure we do not to lex another hash
directive during peek operation.

This fixes PR28921.

https://reviews.llvm.org/D24839

Files:
  include/llvm/MC/MCParser/AsmLexer.h
  lib/MC/MCParser/AsmLexer.cpp
  test/MC/AsmParser/pr28921.s

Index: test/MC/AsmParser/pr28921.s
===================================================================
--- /dev/null
+++ test/MC/AsmParser/pr28921.s
@@ -0,0 +1,8 @@
+// RUN: llvm-mc -triple i386-unknown-unknown %s
+
+# 1 "kernel.S"
+# 1 "<built-in>" 1
+# 1 "kernel.S" 2
+##
+# 10 "kernel.S"
+##
Index: lib/MC/MCParser/AsmLexer.cpp
===================================================================
--- lib/MC/MCParser/AsmLexer.cpp
+++ lib/MC/MCParser/AsmLexer.cpp
@@ -34,6 +34,7 @@
   IsAtStartOfLine = true;
   IsAtStartOfStatement = true;
   IsParsingMSInlineAsm = false;
+  IsPeeking = false;
   AllowAtInIdentifier = !StringRef(MAI.getCommentString()).startswith("@");
 }
 
@@ -492,6 +493,8 @@
   bool SavedAtStartOfLine = IsAtStartOfLine;
   bool SavedAtStartOfStatement = IsAtStartOfStatement;
   bool SavedSkipSpace = SkipSpace;
+  bool SavedIsPeeking = IsPeeking;
+  IsPeeking = true;
 
   std::string SavedErr = getErr();
   SMLoc SavedErrLoc = getErrLoc();
@@ -515,7 +518,7 @@
   IsAtStartOfStatement = SavedAtStartOfStatement;
   CurPtr = SavedCurPtr;
   TokStart = SavedTokStart;
-
+  IsPeeking = SavedIsPeeking;
   return ReadCount;
 }
 
@@ -542,7 +545,7 @@
   // This always consumes at least one character.
   int CurChar = getNextChar();
 
-  if (CurChar == '#' && IsAtStartOfStatement) {
+  if (!IsPeeking && CurChar == '#' && IsAtStartOfStatement) {
     // If this starts with a '#', this may be a cpp
     // hash directive and otherwise a line comment.
     AsmToken TokenBuf[2];
Index: include/llvm/MC/MCParser/AsmLexer.h
===================================================================
--- include/llvm/MC/MCParser/AsmLexer.h
+++ include/llvm/MC/MCParser/AsmLexer.h
@@ -32,7 +32,7 @@
   bool IsAtStartOfLine;
   bool IsAtStartOfStatement;
   bool IsParsingMSInlineAsm;
-
+  bool IsPeeking;
   void operator=(const AsmLexer&) = delete;
   AsmLexer(const AsmLexer&) = delete;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24839.72201.patch
Type: text/x-patch
Size: 1903 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160922/55ad2863/attachment.bin>


More information about the llvm-commits mailing list