[PATCH] D18240: Asm preprocessor fix for unknown hash-lines

Andrew Zhogin via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 17 05:14:08 PDT 2016


andrew.zhogin created this revision.
andrew.zhogin added reviewers: jordan_rose, bkramer.
andrew.zhogin added a subscriber: cfe-commits.

When preprocessing assembly file (with "clang -cc1 -E -x assembler-with-cpp") after every unknown-pragma line, additional empty line is added.
# some comment 1
# some comment 2
# some comment 3
Will be converted into:
# some comment 1

# some comment 2

# some comment 3

It causes debug-info lines shift.
This patch fixes this problem.

http://reviews.llvm.org/D18240

Files:
  lib/Frontend/PrintPreprocessedOutput.cpp
  test/Misc/asm_hash_comments.s

Index: test/Misc/asm_hash_comments.s
===================================================================
--- test/Misc/asm_hash_comments.s
+++ test/Misc/asm_hash_comments.s
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -E -x assembler-with-cpp %s | FileCheck --strict-whitespace %s
+// CHECK: # some comment 1
+// CHECK-NEXT: # some comment 2
+// CHECK-NEXT: # some comment 3
+
+# some comment 1
+# some comment 2
+# some comment 3
+
+
Index: lib/Frontend/PrintPreprocessedOutput.cpp
===================================================================
--- lib/Frontend/PrintPreprocessedOutput.cpp
+++ lib/Frontend/PrintPreprocessedOutput.cpp
@@ -95,6 +95,7 @@
   bool DumpDefines;
   bool UseLineDirectives;
   bool IsFirstFileEntered;
+  bool NewLinesInTokenHandled;
 public:
   PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, bool lineMarkers,
                            bool defines, bool UseLineDirectives)
@@ -108,6 +109,7 @@
     FileType = SrcMgr::C_User;
     Initialized = false;
     IsFirstFileEntered = false;
+    NewLinesInTokenHandled = false;
   }
 
   void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
@@ -161,7 +163,7 @@
   void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr,
                      unsigned ExtraLen=0);
   bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
-  void HandleNewlinesInToken(const char *TokStr, unsigned Len);
+  void HandleNewlinesInToken(const char *TokStr, unsigned Len, bool IsSp);
 
   /// MacroDefined - This hook is called whenever a macro definition is seen.
   void MacroDefined(const Token &MacroNameTok,
@@ -206,11 +208,13 @@
 bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
   // If this line is "close enough" to the original line, just print newlines,
   // otherwise print a #line directive.
+  bool WasNewLines = NewLinesInTokenHandled;
+  NewLinesInTokenHandled = false;
   if (LineNo-CurLine <= 8) {
     if (LineNo-CurLine == 1)
       OS << '\n';
     else if (LineNo == CurLine)
-      return false;    // Spelling line moved, but expansion line didn't.
+      return WasNewLines;    // Spelling line moved, but expansion line didn't.
     else {
       const char *NewLines = "\n\n\n\n\n\n\n\n";
       OS.write(NewLines, LineNo-CurLine);
@@ -533,7 +537,7 @@
 }
 
 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
-                                                     unsigned Len) {
+                                                     unsigned Len, bool IsSp) {
   unsigned NumNewlines = 0;
   for (; Len; --Len, ++TokStr) {
     if (*TokStr != '\n' &&
@@ -554,6 +558,7 @@
   if (NumNewlines == 0) return;
 
   CurLine += NumNewlines;
+  if (IsSp) NewLinesInTokenHandled = true;
 }
 
 
@@ -667,15 +672,19 @@
       // Tokens that can contain embedded newlines need to adjust our current
       // line number.
       if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
-        Callbacks->HandleNewlinesInToken(TokPtr, Len);
+        Callbacks->HandleNewlinesInToken(TokPtr, Len, false);
+      if (Tok.getKind() == tok::eod)
+        Callbacks->HandleNewlinesInToken(TokPtr, Len, true);
     } else {
       std::string S = PP.getSpelling(Tok);
       OS.write(&S[0], S.size());
 
       // Tokens that can contain embedded newlines need to adjust our current
       // line number.
       if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
-        Callbacks->HandleNewlinesInToken(&S[0], S.size());
+        Callbacks->HandleNewlinesInToken(&S[0], S.size(), false);
+      if (Tok.getKind() == tok::eod)
+        Callbacks->HandleNewlinesInToken(&S[0], S.size(), true);
     }
     Callbacks->setEmittedTokensOnThisLine();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18240.50924.patch
Type: text/x-patch
Size: 3721 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160317/793bbe1e/attachment.bin>


More information about the cfe-commits mailing list