diff --git a/clang/include/clang/Lex/HeaderSearch.h b/clang/include/clang/Lex/HeaderSearch.h index 2a4e046be46fd..d3ee4963fced9 100644 --- a/clang/include/clang/Lex/HeaderSearch.h +++ b/clang/include/clang/Lex/HeaderSearch.h @@ -482,7 +482,7 @@ class HeaderSearch { OptionalFileEntryRef LookupFile( StringRef Filename, SourceLocation IncludeLoc, bool isAngled, ConstSearchDirIterator FromDir, ConstSearchDirIterator *CurDir, - ArrayRef> Includers, + ArrayRef> Includers, SmallVectorImpl *SearchPath, SmallVectorImpl *RelativePath, Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool *IsFrameworkFound, bool SkipCache = false, diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp index c5893874e1d32..cfac2f8c4e5a6 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -825,11 +825,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, "trying to build a header unit without a Pre-processor?"); HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); // Relative searches begin from CWD. - const DirectoryEntry *Dir = nullptr; - if (auto DirOrErr = CI.getFileManager().getDirectory(".")) - Dir = *DirOrErr; - SmallVector, 1> CWD; - CWD.push_back({nullptr, Dir}); + auto Dir = CI.getFileManager().getOptionalDirectoryRef("."); + SmallVector, 1> CWD; + CWD.push_back({nullptr, *Dir}); OptionalFileEntryRef FE = HS.LookupFile(FileName, SourceLocation(), /*Angled*/ Input.getKind().getHeaderUnitKind() == diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp index 7df1ca16f67ce..3366f158fd4f7 100644 --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -863,7 +863,7 @@ diagnoseFrameworkInclude(DiagnosticsEngine &Diags, SourceLocation IncludeLoc, OptionalFileEntryRef HeaderSearch::LookupFile( StringRef Filename, SourceLocation IncludeLoc, bool isAngled, ConstSearchDirIterator FromDir, ConstSearchDirIterator *CurDirArg, - ArrayRef> Includers, + ArrayRef> Includers, SmallVectorImpl *SearchPath, SmallVectorImpl *RelativePath, Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool *IsFrameworkFound, bool SkipCache, @@ -918,7 +918,7 @@ OptionalFileEntryRef HeaderSearch::LookupFile( // Concatenate the requested file onto the directory. // FIXME: Portability. Filename concatenation should be in sys::Path. - TmpDir = IncluderAndDir.second->getName(); + TmpDir = IncluderAndDir.second.getName(); TmpDir.push_back('/'); TmpDir.append(Filename.begin(), Filename.end()); @@ -957,7 +957,7 @@ OptionalFileEntryRef HeaderSearch::LookupFile( ToHFI.Framework = Framework; if (SearchPath) { - StringRef SearchPathRef(IncluderAndDir.second->getName()); + StringRef SearchPathRef(IncluderAndDir.second.getName()); SearchPath->clear(); SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); } @@ -967,7 +967,7 @@ OptionalFileEntryRef HeaderSearch::LookupFile( } if (First) { diagnoseFrameworkInclude(Diags, IncludeLoc, - IncluderAndDir.second->getName(), Filename, + IncluderAndDir.second.getName(), Filename, &FE->getFileEntry()); return FE; } @@ -1122,7 +1122,7 @@ OptionalFileEntryRef HeaderSearch::LookupFile( bool FoundByHeaderMap = !IsMapped ? false : *IsMapped; if (!Includers.empty()) diagnoseFrameworkInclude( - Diags, IncludeLoc, Includers.front().second->getName(), Filename, + Diags, IncludeLoc, Includers.front().second.getName(), Filename, &File->getFileEntry(), isAngled, FoundByHeaderMap); // Remember this location for the next lookup we do. diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 1a5398e3adea6..b3ce92f1699da 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -935,12 +935,11 @@ OptionalFileEntryRef Preprocessor::LookupFile( // If the header lookup mechanism may be relative to the current inclusion // stack, record the parent #includes. - SmallVector, 16> - Includers; + SmallVector, 16> Includers; bool BuildSystemModule = false; if (!FromDir && !FromFile) { FileID FID = getCurrentFileLexer()->getFileID(); - const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID); + OptionalFileEntryRef FileEnt = SourceMgr.getFileEntryRefForID(FID); // If there is no file entry associated with this file, it must be the // predefines buffer or the module includes buffer. Any other file is not @@ -958,11 +957,13 @@ OptionalFileEntryRef Preprocessor::LookupFile( if (FID == SourceMgr.getMainFileID() && MainFileDir) { Includers.push_back(std::make_pair(nullptr, *MainFileDir)); BuildSystemModule = getCurrentModule()->IsSystem; - } else if ((FileEnt = - SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))) - Includers.push_back(std::make_pair(FileEnt, *FileMgr.getDirectory("."))); + } else if ((FileEnt = SourceMgr.getFileEntryRefForID( + SourceMgr.getMainFileID()))) { + auto CWD = FileMgr.getOptionalDirectoryRef("."); + Includers.push_back(std::make_pair(*FileEnt, *CWD)); + } } else { - Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir())); + Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir())); } // MSVC searches the current include stack from top to bottom for @@ -972,7 +973,7 @@ OptionalFileEntryRef Preprocessor::LookupFile( for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) { if (IsFileLexer(ISEntry)) if ((FileEnt = ISEntry.ThePPLexer->getFileEntry())) - Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir())); + Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir())); } } } diff --git a/clang/test/Modules/Inputs/filename/a.h b/clang/test/Modules/Inputs/filename/a.h deleted file mode 100644 index 8f896a9ba8f41..0000000000000 --- a/clang/test/Modules/Inputs/filename/a.h +++ /dev/null @@ -1 +0,0 @@ -const char *p = __FILE__; diff --git a/clang/test/Modules/Inputs/filename/module.map b/clang/test/Modules/Inputs/filename/module.map deleted file mode 100644 index ff164ad7bac8e..0000000000000 --- a/clang/test/Modules/Inputs/filename/module.map +++ /dev/null @@ -1,3 +0,0 @@ -module "A" { - header "a.h" -} diff --git a/clang/test/Modules/filename.cpp b/clang/test/Modules/filename.cpp index e2b5ad141891f..7c42a7eddee38 100644 --- a/clang/test/Modules/filename.cpp +++ b/clang/test/Modules/filename.cpp @@ -1,8 +1,17 @@ -// RUN: cd %S -// RUN: %clang_cc1 -I. -fmodule-name=A -fmodule-map-file=%S/Inputs/filename/module.map %s -E | FileCheck %s +// RUN: rm -rf %t +// RUN: split-file %s %t -#include "Inputs/filename/a.h" +//--- include/a.h +const char *p = __FILE__; +//--- include/module.modulemap +module "A" { header "a.h" } +//--- src/tu.cpp +#include "a.h" + +// RUN: cd %t +// RUN: %clang_cc1 -I ./include -fmodule-name=A -fmodule-map-file=%t/include/module.modulemap %t/src/tu.cpp -E | FileCheck %s // Make sure that headers that are referenced by module maps have __FILE__ -// reflect the include path they were found with. -// CHECK: const char *p = "./Inputs/filename/a.h" +// reflect the include path they were found with. (We make sure they cannot be +// found relative to the includer.) +// CHECK: const char *p = "./include{{/|\\\\}}a.h"