From: Richard Smith Date: Fri, 8 Apr 2016 00:09:53 +0000 (+0000) Subject: [modules] Don't write @import in -E output if the current language mode doesn't X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1160cb7defb1c674eeed825d8c274666015fec6c;p=clang [modules] Don't write @import in -E output if the current language mode doesn't support @import; use the form as written instead. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@265756 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp index 5dd7980d81..9524b58e3a 100644 --- a/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/lib/Frontend/PrintPreprocessedOutput.cpp @@ -326,8 +326,18 @@ void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc, if (Imported) { startNewLineIfNeeded(); MoveToLine(HashLoc); - OS << "@import " << Imported->getFullModuleName() << ";" - << " /* clang -E: implicit import for \"" << File->getName() << "\" */"; + if (PP.getLangOpts().ObjC2) { + OS << "@import " << Imported->getFullModuleName() << ";" + << " /* clang -E: implicit import for \"" << File->getName() + << "\" */"; + } else { + // FIXME: Preseve whether this was a + // #include/#include_next/#include_macros/#import. + OS << "#include " + << (IsAngled ? '<' : '"') + << FileName + << (IsAngled ? '>' : '"'); + } // Since we want a newline after the @import, but not a #, start a new // line immediately. EmittedTokensOnThisLine = true; diff --git a/lib/Frontend/Rewrite/InclusionRewriter.cpp b/lib/Frontend/Rewrite/InclusionRewriter.cpp index ca8226251f..b761c34fcb 100644 --- a/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -450,7 +450,9 @@ bool InclusionRewriter::Process(FileID FileId, WriteLineInfo(FileName, Line - 1, FileType, ""); StringRef LineInfoExtra; SourceLocation Loc = HashToken.getLocation(); - if (const Module *Mod = FindModuleAtLocation(Loc)) + if (const Module *Mod = PP.getLangOpts().ObjC2 + ? FindModuleAtLocation(Loc) + : nullptr) WriteImplicitModuleImport(Mod); else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) { // include and recursively process the file diff --git a/test/Modules/preprocess.cpp b/test/Modules/preprocess.cpp new file mode 100644 index 0000000000..aa28191722 --- /dev/null +++ b/test/Modules/preprocess.cpp @@ -0,0 +1,22 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs -x c++ -E %s | \ +// RUN: FileCheck -strict-whitespace %s --check-prefix=CHECK --check-prefix=CXX +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs -x objective-c -E %s | \ +// RUN: FileCheck -strict-whitespace %s --check-prefix=CHECK --check-prefix=OBJC +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs -x c++ -E -frewrite-includes %s | \ +// RUN: FileCheck -strict-whitespace %s --check-prefix=CHECK --check-prefix=CXX +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs -x objective-c -E -frewrite-includes %s | \ +// RUN: FileCheck -strict-whitespace %s --check-prefix=CHECK --check-prefix=OBJC +#include "dummy.h" +#include "dummy.h" +foo bar baz + +// The weird {{ }} here is to prevent the -frewrite-includes test from matching its own CHECK lines. + +// CXX: #include{{ }}"dummy.h" +// CXX: #include{{ }}"dummy.h" +// CXX: foo bar baz + +// OBJC: @import{{ }}dummy; /* clang +// OBJC: @import{{ }}dummy; /* clang +// OBJC: foo bar baz