Skip to content

Commit

Permalink
[NFC][SYCL] Remove multiple definitions of ComputeValidFooterFileID. (#…
Browse files Browse the repository at this point in the history
…14993)

The same functionality was needed at the 2 different places, and I
inadvertently created 2 functions. This patch refactors it.
  • Loading branch information
zahiraam authored Aug 13, 2024
1 parent f5bbb7a commit cad9a1b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
13 changes: 13 additions & 0 deletions clang/include/clang/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,19 @@ class SourceManager : public RefCountedBase<SourceManager> {
/// Get the file ID for the precompiled preamble if there is one.
FileID getPreambleFileID() const { return PreambleFileID; }

/// Get the file ID for the integration footer.
FileID ComputeValidFooterFileID(StringRef Footer) {
FileID FooterFileID;
llvm::Expected<FileEntryRef> ExpectedFileRef =
getFileManager().getFileRef(Footer);
if (ExpectedFileRef) {
FooterFileID = getOrCreateFileID(ExpectedFileRef.get(),
SrcMgr::CharacteristicKind::C_User);
}
assert(FooterFileID.isValid() && "expecting a valid footer FileID");
return FooterFileID;
}

//===--------------------------------------------------------------------===//
// Methods to create new FileID's and macro expansions.
//===--------------------------------------------------------------------===//
Expand Down
14 changes: 1 addition & 13 deletions clang/lib/Frontend/PrintPreprocessedOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,26 +904,14 @@ struct UnknownPragmaHandler : public PragmaHandler {
};
} // end anonymous namespace

FileID ComputeValidFooterFileID(SourceManager &SM, StringRef Footer) {
FileID FooterFileID;
llvm::Expected<FileEntryRef> ExpectedFileRef =
SM.getFileManager().getFileRef(Footer);
if (ExpectedFileRef) {
FooterFileID = SM.getOrCreateFileID(ExpectedFileRef.get(),
SrcMgr::CharacteristicKind::C_User);
}
assert(FooterFileID.isValid() && "expecting a valid footer FileID");
return FooterFileID;
}

static void PrintIncludeFooter(Preprocessor &PP, SourceLocation Loc,
std::string Footer,
PrintPPOutputPPCallbacks *Callbacks) {
SourceManager &SourceMgr = PP.getSourceManager();
PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
if (UserLoc.isInvalid())
return;
FileID FooterFileID = ComputeValidFooterFileID(SourceMgr, Footer);
FileID FooterFileID = SourceMgr.ComputeValidFooterFileID(Footer);
StringRef FooterContentBuffer = SourceMgr.getBufferData(FooterFileID);
// print out the name of the integration footer.
Callbacks->WriteFooterInfo(Footer);
Expand Down
16 changes: 2 additions & 14 deletions clang/lib/Lex/PPLexerChange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,18 +326,6 @@ void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) {
}
}

static FileID ComputeValidFooterFileID(SourceManager &SM, StringRef Footer) {
FileID FooterFileID;
llvm::Expected<FileEntryRef> ExpectedFileRef =
SM.getFileManager().getFileRef(Footer);
if (ExpectedFileRef) {
FooterFileID = SM.getOrCreateFileID(ExpectedFileRef.get(),
SrcMgr::CharacteristicKind::C_User);
}
assert(FooterFileID.isValid() && "expecting a valid footer FileID");
return FooterFileID;
}

/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
/// the current file. This either returns the EOF token or pops a level off
/// the include stack and keeps going.
Expand Down Expand Up @@ -552,8 +540,8 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
SourceManager &SourceMgr = getSourceManager();
SourceLocation Loc = CurLexer->getFileLoc();

FileID FooterFileID = ComputeValidFooterFileID(
SourceMgr, getPreprocessorOpts().IncludeFooter);
FileID FooterFileID =
SourceMgr.ComputeValidFooterFileID(getPreprocessorOpts().IncludeFooter);
if (!FooterFileID.isInvalid() && !IncludeFooterProcessed) {
IncludeFooterProcessed = true;
// Mark the footer file as included
Expand Down

0 comments on commit cad9a1b

Please sign in to comment.