r350072 - [clang-cl] Treat inputs as C++ with /E, like MSVC

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 26 13:04:08 PST 2018


Author: rnk
Date: Wed Dec 26 13:04:08 2018
New Revision: 350072

URL: http://llvm.org/viewvc/llvm-project?rev=350072&view=rev
Log:
[clang-cl] Treat inputs as C++ with /E, like MSVC

midl invokes the compiler on .idl files with /E. Before this change, we
would treat unrecognized inputs as object files. Now we pre-process to
stdout as expected. I checked that MSVC defines __cplusplus when invoked
this way, so treating the input as C++ seems like the right thing to do.

After this change, I was able to run midl like this with clang-cl:
$ midl -cpp_cmd clang-cl.exe foo.idl

Things worked for the example IDL file in the Microsoft documentation,
but beyond that, I don't know if this will work well.

Fixes PR40140

Added:
    cfe/trunk/test/Driver/cl-idl.cpp
Modified:
    cfe/trunk/lib/Driver/Driver.cpp

Modified: cfe/trunk/lib/Driver/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Driver.cpp?rev=350072&r1=350071&r2=350072&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Driver.cpp (original)
+++ cfe/trunk/lib/Driver/Driver.cpp Wed Dec 26 13:04:08 2018
@@ -2057,7 +2057,8 @@ void Driver::BuildInputs(const ToolChain
           Ty = types::TY_C;
         } else {
           // Otherwise lookup by extension.
-          // Fallback is C if invoked as C preprocessor or Object otherwise.
+          // Fallback is C if invoked as C preprocessor, C++ if invoked with
+          // clang-cl /E, or Object otherwise.
           // We use a host hook here because Darwin at least has its own
           // idea of what .s is.
           if (const char *Ext = strrchr(Value, '.'))
@@ -2066,6 +2067,8 @@ void Driver::BuildInputs(const ToolChain
           if (Ty == types::TY_INVALID) {
             if (CCCIsCPP())
               Ty = types::TY_C;
+            else if (IsCLMode() && Args.hasArgNoClaim(options::OPT_E))
+              Ty = types::TY_CXX;
             else
               Ty = types::TY_Object;
           }

Added: cfe/trunk/test/Driver/cl-idl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/cl-idl.cpp?rev=350072&view=auto
==============================================================================
--- cfe/trunk/test/Driver/cl-idl.cpp (added)
+++ cfe/trunk/test/Driver/cl-idl.cpp Wed Dec 26 13:04:08 2018
@@ -0,0 +1,18 @@
+// Note: %s must be preceded by --, otherwise it may be interpreted as a
+// command-line option, e.g. on Mac where %s is commonly under /Users.
+
+// Test that 'clang-cl /E' treats inputs as C++ if the extension is
+// unrecognized. midl relies on this. See PR40140.
+
+// Use a plain .cpp extension first.
+// RUN: %clang_cl /E -- %s | FileCheck %s
+
+// Copy to use .idl as the extension.
+// RUN: cp %s %t.idl
+// RUN: %clang_cl /E -- %t.idl | FileCheck %s
+
+#ifdef __cplusplus
+struct IsCPlusPlus {};
+#endif
+
+// CHECK: struct IsCPlusPlus {};




More information about the cfe-commits mailing list