Skip to content

Commit

Permalink
[clang][lex] NFC: De-duplicate some #include_next logic
Browse files Browse the repository at this point in the history
This patch addresses a FIXME and de-duplicates some `#include_next` logic

Depends on D119714.

Reviewed By: ahoppen

Differential Revision: https://reviews.llvm.org/D119716
  • Loading branch information
jansvoboda11 committed Feb 15, 2022
1 parent c6f8704 commit a081a06
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 54 deletions.
4 changes: 4 additions & 0 deletions clang/include/clang/Lex/Preprocessor.h
Expand Up @@ -2200,6 +2200,10 @@ class Preprocessor {
/// Returns true if successful.
bool EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II);

/// Get the directory and file from which to start \#include_next lookup.
std::pair<const DirectoryLookup *, const FileEntry *>
getIncludeNextStart(const Token &IncludeNextTok) const;

/// Install the standard preprocessor pragmas:
/// \#pragma GCC poison/system_header/dependency and \#pragma once.
void RegisterBuiltinPragmas();
Expand Down
66 changes: 38 additions & 28 deletions clang/lib/Lex/PPDirectives.cpp
Expand Up @@ -1778,6 +1778,41 @@ bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts,
return true;
}

std::pair<const DirectoryLookup *, const FileEntry *>
Preprocessor::getIncludeNextStart(const Token &IncludeNextTok) const {
// #include_next is like #include, except that we start searching after
// the current found directory. If we can't do this, issue a
// diagnostic.
const DirectoryLookup *Lookup = CurDirLookup;
const FileEntry *LookupFromFile = nullptr;

if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
// If the main file is a header, then it's either for PCH/AST generation,
// or libclang opened it. Either way, handle it as a normal include below
// and do not complain about include_next.
} else if (isInPrimaryFile()) {
Lookup = nullptr;
Diag(IncludeNextTok, diag::pp_include_next_in_primary);
} else if (CurLexerSubmodule) {
// Start looking up in the directory *after* the one in which the current
// file would be found, if any.
assert(CurPPLexer && "#include_next directive in macro?");
LookupFromFile = CurPPLexer->getFileEntry();
Lookup = nullptr;
} else if (!Lookup) {
// The current file was not found by walking the include path. Either it
// is the primary file (handled above), or it was found by absolute path,
// or it was found relative to such a file.
// FIXME: Track enough information so we know which case we're in.
Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
} else {
// Start looking up in the next directory.
++Lookup;
}

return {Lookup, LookupFromFile};
}

/// HandleIncludeDirective - The "\#include" tokens have just been read, read
/// the file to be included from the lexer, then include it! This is a common
/// routine with functionality shared between \#include, \#include_next and
Expand Down Expand Up @@ -2375,34 +2410,9 @@ void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
Token &IncludeNextTok) {
Diag(IncludeNextTok, diag::ext_pp_include_next_directive);

// #include_next is like #include, except that we start searching after
// the current found directory. If we can't do this, issue a
// diagnostic.
const DirectoryLookup *Lookup = CurDirLookup;
const FileEntry *LookupFromFile = nullptr;
if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
// If the main file is a header, then it's either for PCH/AST generation,
// or libclang opened it. Either way, handle it as a normal include below
// and do not complain about include_next.
} else if (isInPrimaryFile()) {
Lookup = nullptr;
Diag(IncludeNextTok, diag::pp_include_next_in_primary);
} else if (CurLexerSubmodule) {
// Start looking up in the directory *after* the one in which the current
// file would be found, if any.
assert(CurPPLexer && "#include_next directive in macro?");
LookupFromFile = CurPPLexer->getFileEntry();
Lookup = nullptr;
} else if (!Lookup) {
// The current file was not found by walking the include path. Either it
// is the primary file (handled above), or it was found by absolute path,
// or it was found relative to such a file.
// FIXME: Track enough information so we know which case we're in.
Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
} else {
// Start looking up in the next directory.
++Lookup;
}
const DirectoryLookup *Lookup;
const FileEntry *LookupFromFile;
std::tie(Lookup, LookupFromFile) = getIncludeNextStart(IncludeNextTok);

return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
LookupFromFile);
Expand Down
29 changes: 3 additions & 26 deletions clang/lib/Lex/PPMacroExpansion.cpp
Expand Up @@ -1249,32 +1249,9 @@ bool Preprocessor::EvaluateHasInclude(Token &Tok, IdentifierInfo *II) {
}

bool Preprocessor::EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II) {
// __has_include_next is like __has_include, except that we start
// searching after the current found directory. If we can't do this,
// issue a diagnostic.
// FIXME: Factor out duplication with
// Preprocessor::HandleIncludeNextDirective.
const DirectoryLookup *Lookup = CurDirLookup;
const FileEntry *LookupFromFile = nullptr;
if (isInPrimaryFile() && getLangOpts().IsHeaderFile) {
// If the main file is a header, then it's either for PCH/AST generation,
// or libclang opened it. Either way, handle it as a normal include below
// and do not complain about __has_include_next.
} else if (isInPrimaryFile()) {
Lookup = nullptr;
Diag(Tok, diag::pp_include_next_in_primary);
} else if (getCurrentLexerSubmodule()) {
// Start looking up in the directory *after* the one in which the current
// file would be found, if any.
assert(getCurrentLexer() && "#include_next directive in macro?");
LookupFromFile = getCurrentLexer()->getFileEntry();
Lookup = nullptr;
} else if (!Lookup) {
Diag(Tok, diag::pp_include_next_absolute_path);
} else {
// Start looking up in the next directory.
++Lookup;
}
const DirectoryLookup *Lookup;
const FileEntry *LookupFromFile;
std::tie(Lookup, LookupFromFile) = getIncludeNextStart(Tok);

return EvaluateHasIncludeCommon(Tok, II, *this, Lookup, LookupFromFile);
}
Expand Down

0 comments on commit a081a06

Please sign in to comment.