[cfe-commits] r73365 - in /cfe/trunk: lib/Frontend/PrintPreprocessedOutput.cpp test/Preprocessor/print_line_count.c

Chris Lattner sabre at nondot.org
Sun Jun 14 18:25:23 PDT 2009


Author: lattner
Date: Sun Jun 14 20:25:23 2009
New Revision: 73365

URL: http://llvm.org/viewvc/llvm-project?rev=73365&view=rev
Log:
Fix PR2741 by making our newline tracking be aware of newlines that
can occur in the middle of comment tokens.

Added:
    cfe/trunk/test/Preprocessor/print_line_count.c
Modified:
    cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp

Modified: cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp?rev=73365&r1=73364&r2=73365&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp (original)
+++ cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp Sun Jun 14 20:25:23 2009
@@ -123,6 +123,8 @@
   }
   void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
   
+  void HandleNewlinesInToken(const char *TokStr, unsigned Len);
+  
   /// MacroDefined - This hook is called whenever a macro definition is seen.
   void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
   
@@ -327,6 +329,29 @@
   return true;
 }
 
+void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
+                                                     unsigned Len) {
+  unsigned NumNewlines = 0;
+  for (; Len; --Len, ++TokStr) {
+    if (*TokStr != '\n' &&
+        *TokStr != '\r')
+      continue;
+  
+    ++NumNewlines;
+    
+    // If we have \n\r or \r\n, skip both and count as one line.
+    if (Len != 1 &&
+        (TokStr[1] == '\n' || TokStr[1] == '\r') &&
+        TokStr[0] != TokStr[1])
+      ++TokStr, --Len;
+  }
+  
+  if (NumNewlines == 0) return;
+  
+//  CurLine += NumNewlines;
+}
+
+
 namespace {
 struct UnknownPragmaHandler : public PragmaHandler {
   const char *Prefix;
@@ -382,9 +407,19 @@
       const char *TokPtr = Buffer;
       unsigned Len = PP.getSpelling(Tok, TokPtr);
       OS.write(TokPtr, Len);
+      
+      // Tokens that can contain embedded newlines need to adjust our current
+      // line number. 
+      if (Tok.getKind() == tok::comment)
+        Callbacks->HandleNewlinesInToken(TokPtr, Len);
     } 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)
+        Callbacks->HandleNewlinesInToken(&S[0], S.size());
     }
     Callbacks->SetEmittedTokensOnThisLine();
     

Added: cfe/trunk/test/Preprocessor/print_line_count.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/print_line_count.c?rev=73365&view=auto

==============================================================================
--- cfe/trunk/test/Preprocessor/print_line_count.c (added)
+++ cfe/trunk/test/Preprocessor/print_line_count.c Sun Jun 14 20:25:23 2009
@@ -0,0 +1,4 @@
+/* RUN: clang -E -C -P %s | wc -l | grep 4
+   PR2741
+   comment */ 
+y





More information about the cfe-commits mailing list