[modules] Don't write @import in -E output if the current language mode doesn't

support @import; use the form as written instead.

llvm-svn: 265756
This commit is contained in:
Richard Smith 2016-04-08 00:09:53 +00:00
parent e0a7ffa6cb
commit 466a15ef7f
3 changed files with 37 additions and 3 deletions

View File

@ -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 #<line>, start a new
// line immediately.
EmittedTokensOnThisLine = true;

View File

@ -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

View File

@ -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