r189511 - Handle -D arguments ending in a backslash.

Eli Friedman eli.friedman at gmail.com
Wed Aug 28 13:35:38 PDT 2013


Author: efriedma
Date: Wed Aug 28 15:35:38 2013
New Revision: 189511

URL: http://llvm.org/viewvc/llvm-project?rev=189511&view=rev
Log:
Handle -D arguments ending in a backslash.

We translate these into #define directives; to preserve gcc-compatible
semantics (where the expanded macro includes the backslash), we add
an extra "\\\n" to the end of the synthesized "#define".

<rdar://problem/14810220>

Added:
    cfe/trunk/test/Preprocessor/macro_backslash.c
Modified:
    cfe/trunk/lib/Frontend/InitPreprocessor.cpp

Modified: cfe/trunk/lib/Frontend/InitPreprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/InitPreprocessor.cpp?rev=189511&r1=189510&r2=189511&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/InitPreprocessor.cpp (original)
+++ cfe/trunk/lib/Frontend/InitPreprocessor.cpp Wed Aug 28 15:35:38 2013
@@ -29,6 +29,12 @@
 #include "llvm/Support/Path.h"
 using namespace clang;
 
+static bool MacroBodyEndsInBackslash(StringRef MacroBody) {
+  while (!MacroBody.empty() && isWhitespace(MacroBody.back()))
+    MacroBody = MacroBody.drop_back();
+  return !MacroBody.empty() && MacroBody.back() == '\\';
+}
+
 // Append a #define line to Buf for Macro.  Macro should be of the form XXX,
 // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
 // "#define XXX Y z W".  To get a #define with no value, use "XXX=".
@@ -43,7 +49,14 @@ static void DefineBuiltinMacro(MacroBuil
     if (End != StringRef::npos)
       Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
         << MacroName;
-    Builder.defineMacro(MacroName, MacroBody.substr(0, End));
+    MacroBody = MacroBody.substr(0, End);
+    // We handle macro bodies which end in a backslash by appending an extra
+    // backslash+newline.  This makes sure we don't accidentally treat the
+    // backslash as a line continuation marker.
+    if (MacroBodyEndsInBackslash(MacroBody))
+      Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n");
+    else
+      Builder.defineMacro(MacroName, MacroBody);
   } else {
     // Push "macroname 1".
     Builder.defineMacro(Macro);

Added: cfe/trunk/test/Preprocessor/macro_backslash.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/macro_backslash.c?rev=189511&view=auto
==============================================================================
--- cfe/trunk/test/Preprocessor/macro_backslash.c (added)
+++ cfe/trunk/test/Preprocessor/macro_backslash.c Wed Aug 28 15:35:38 2013
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -E %s -Dfoo='bar\' | FileCheck %s
+// CHECK: TTA bar\ TTB
+TTA foo TTB





More information about the cfe-commits mailing list