diff options
author | Volodymyr Sapsai <vsapsai@apple.com> | 2018-04-13 17:43:15 +0000 |
---|---|---|
committer | Volodymyr Sapsai <vsapsai@apple.com> | 2018-04-13 17:43:15 +0000 |
commit | 1f70bddb838837afff2fa74475e7bff101691557 (patch) | |
tree | 981b01c22a415922d0a563478e5830cf987d6a0e /clang/lib | |
parent | ddf3db9b5eddc6c7cc75fd30eea545c22ad70dcd (diff) | |
download | bcm5719-llvm-1f70bddb838837afff2fa74475e7bff101691557.tar.gz bcm5719-llvm-1f70bddb838837afff2fa74475e7bff101691557.zip |
Fix evaluation of `__has_include_next` during -frewrite-includes.
`__has_include_next` requires correct DirectoryLookup for being
evaluated correctly. We were using Preprocessor::GetCurDirLookup() but
we were calling it after the preprocessor finished its work. And in this
case CurDirLookup is always nullptr which makes `__has_include_next`
behave as `__has_include`.
Fix by storing and using CurDirLookup when preprocessor enters a file,
not when we rewrite the includes.
rdar://problem/36305026
Reviewers: bkramer
Reviewed By: bkramer
Subscribers: jkorous-apple, cfe-commits
Differential Revision: https://reviews.llvm.org/D45603
llvm-svn: 330041
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Frontend/Rewrite/InclusionRewriter.cpp | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp index e0477069b34..3b8d792e3af 100644 --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -32,8 +32,10 @@ class InclusionRewriter : public PPCallbacks { struct IncludedFile { FileID Id; SrcMgr::CharacteristicKind FileType; - IncludedFile(FileID Id, SrcMgr::CharacteristicKind FileType) - : Id(Id), FileType(FileType) {} + const DirectoryLookup *DirLookup; + IncludedFile(FileID Id, SrcMgr::CharacteristicKind FileType, + const DirectoryLookup *DirLookup) + : Id(Id), FileType(FileType), DirLookup(DirLookup) {} }; Preprocessor &PP; ///< Used to find inclusion directives. SourceManager &SM; ///< Used to read and manage source files. @@ -54,7 +56,8 @@ class InclusionRewriter : public PPCallbacks { public: InclusionRewriter(Preprocessor &PP, raw_ostream &OS, bool ShowLineMarkers, bool UseLineDirectives); - void Process(FileID FileId, SrcMgr::CharacteristicKind FileType); + void Process(FileID FileId, SrcMgr::CharacteristicKind FileType, + const DirectoryLookup *DirLookup); void setPredefinesBuffer(const llvm::MemoryBuffer *Buf) { PredefinesBuffer = Buf; } @@ -156,8 +159,9 @@ void InclusionRewriter::FileChanged(SourceLocation Loc, // we didn't reach this file (eg: the main file) via an inclusion directive return; FileID Id = FullSourceLoc(Loc, SM).getFileID(); - auto P = FileIncludes.insert(std::make_pair( - LastInclusionLocation.getRawEncoding(), IncludedFile(Id, NewFileType))); + auto P = FileIncludes.insert( + std::make_pair(LastInclusionLocation.getRawEncoding(), + IncludedFile(Id, NewFileType, PP.GetCurDirLookup()))); (void)P; assert(P.second && "Unexpected revisitation of the same include directive"); LastInclusionLocation = SourceLocation(); @@ -408,7 +412,7 @@ bool InclusionRewriter::HandleHasInclude( Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir())); // FIXME: Why don't we call PP.LookupFile here? const FileEntry *File = PP.getHeaderSearchInfo().LookupFile( - Filename, SourceLocation(), isAngled, nullptr, CurDir, Includers, nullptr, + Filename, SourceLocation(), isAngled, Lookup, CurDir, Includers, nullptr, nullptr, nullptr, nullptr, nullptr); FileExists = File != nullptr; @@ -418,7 +422,8 @@ bool InclusionRewriter::HandleHasInclude( /// Use a raw lexer to analyze \p FileId, incrementally copying parts of it /// and including content of included files recursively. void InclusionRewriter::Process(FileID FileId, - SrcMgr::CharacteristicKind FileType) { + SrcMgr::CharacteristicKind FileType, + const DirectoryLookup *DirLookup) { bool Invalid; const MemoryBuffer &FromFile = *SM.getBuffer(FileId, &Invalid); assert(!Invalid && "Attempting to process invalid inclusion"); @@ -475,7 +480,7 @@ void InclusionRewriter::Process(FileID FileId, << Mod->getFullModuleName(true) << "\n"; // Include and recursively process the file. - Process(Inc->Id, Inc->FileType); + Process(Inc->Id, Inc->FileType, Inc->DirLookup); if (Mod) OS << "#pragma clang module end /*" @@ -532,11 +537,10 @@ void InclusionRewriter::Process(FileID FileId, // Rewrite __has_include_next(x) } else if (RawToken.getIdentifierInfo()->isStr( "__has_include_next")) { - const DirectoryLookup *Lookup = PP.GetCurDirLookup(); - if (Lookup) - ++Lookup; + if (DirLookup) + ++DirLookup; - if (!HandleHasInclude(FileId, RawLex, Lookup, RawToken, + if (!HandleHasInclude(FileId, RawLex, DirLookup, RawToken, HasFile)) continue; } else { @@ -621,7 +625,7 @@ void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, Rewrite->handleModuleBegin(Tok); } while (Tok.isNot(tok::eof)); Rewrite->setPredefinesBuffer(SM.getBuffer(PP.getPredefinesFileID())); - Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User); - Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User); + Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User, nullptr); + Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User, nullptr); OS->flush(); } |