diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp index 5dd7980d81bf..9524b58e3a2b 100644 --- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/clang/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/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index ca8226251fd9..b761c34fcbde 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/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/clang/test/Modules/preprocess.cpp b/clang/test/Modules/preprocess.cpp new file mode 100644 index 000000000000..aa28191722ad --- /dev/null +++ b/clang/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