]> granicus.if.org Git - clang/commitdiff
Preprocessor: don't keep comments under -traditional-cpp.
authorJordan Rose <jordan_rose@apple.com>
Tue, 5 Mar 2013 23:54:55 +0000 (23:54 +0000)
committerJordan Rose <jordan_rose@apple.com>
Tue, 5 Mar 2013 23:54:55 +0000 (23:54 +0000)
This patch is designed for minimal intrusion into normal preprocessing
and compilation; under -E -traditional-cpp, the lexer will still
generate tok::comment nodes since it is preserving all whitespace, but
the output printer will then throw it away.

<rdar://problem/13338680>

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@176534 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Frontend/PrintPreprocessedOutput.cpp
test/Preprocessor/traditional-cpp.c

index 58bbfd3f8af8a58837442b92560211b3c7941494..d894939b4b489afbd5044e461014cd03931b7238 100644 (file)
@@ -515,6 +515,9 @@ struct UnknownPragmaHandler : public PragmaHandler {
 static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
                                     PrintPPOutputPPCallbacks *Callbacks,
                                     raw_ostream &OS) {
+  bool DropComments = PP.getLangOpts().TraditionalCPP &&
+                      !PP.getCommentRetentionState();
+
   char Buffer[256];
   Token PrevPrevTok, PrevTok;
   PrevPrevTok.startToken();
@@ -537,7 +540,13 @@ static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
       OS << ' ';
     }
 
-    if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
+    if (DropComments && Tok.is(tok::comment)) {
+      // Skip comments. Normally the preprocessor does not generate
+      // tok::comment nodes at all when not keeping comments, but under
+      // -traditional-cpp the lexer keeps /all/ whitespace, including comments.
+      SourceLocation StartLoc = Tok.getLocation();
+      Callbacks->MoveToLine(StartLoc.getLocWithOffset(Tok.getLength()));
+    } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
       OS << II->getName();
     } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
                Tok.getLiteralData()) {
index 0396c9b126cd829a5a19b2e547e47e337c62cc3e..4c4633e039209b8ac91d4f0430cccf5aa88038db 100644 (file)
@@ -5,8 +5,14 @@
 /*
  RUN: %clang_cc1 -traditional-cpp %s -E -o %t
  RUN: FileCheck -strict-whitespace < %t %s
+ RUN: %clang_cc1 -traditional-cpp %s -E -C | FileCheck -check-prefix=CHECK-COMMENTS %s
 */
 
+/* -traditional-cpp should eliminate all C89 comments. */
+/* CHECK-NOT: /*
+ * CHECK-COMMENTS: {{^}}/* -traditional-cpp should eliminate all C89 comments. *{{/$}}
+ */
+
 /* CHECK: {{^}}foo // bar{{$}}
  */
 foo // bar