https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/109583
Without this patch, several callers of LoadFromASTFile construct an instance of std::string to be passed as FileName, only to be converted back to StringRef when LoadFromASTFile calls ReadAST. This patch changes the type of FileName to StringRef and updates the callers. >From 5818c6dbd5a14d26a5d80652b90e14aec1ca1103 Mon Sep 17 00:00:00 2001 From: Kazu Hirata <k...@google.com> Date: Sat, 1 Jun 2024 12:54:44 -0700 Subject: [PATCH] [Frontend] Teach LoadFromASTFile to take FileName by StringRef (NFC) Without this patch, several callers of LoadFromASTFile construct an instance of std::string to be passed as FileName, only to be converted back to StringRef when LoadFromASTFile calls ReadAST. This patch changes the type of FileName to StringRef and updates the callers. --- clang/include/clang/Frontend/ASTUnit.h | 4 ++-- clang/lib/CrossTU/CrossTranslationUnit.cpp | 6 +++--- clang/lib/Frontend/ASTUnit.cpp | 2 +- clang/lib/Frontend/FrontendAction.cpp | 10 +++++----- clang/tools/c-index-test/core_main.cpp | 12 ++++++------ .../tools/clang-extdef-mapping/ClangExtDefMapGen.cpp | 2 +- clang/unittests/Frontend/ASTUnitTest.cpp | 4 ++-- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/clang/include/clang/Frontend/ASTUnit.h b/clang/include/clang/Frontend/ASTUnit.h index 080844893c13c9..8cefae8587aa34 100644 --- a/clang/include/clang/Frontend/ASTUnit.h +++ b/clang/include/clang/Frontend/ASTUnit.h @@ -692,8 +692,8 @@ class ASTUnit { /// /// \returns - The initialized ASTUnit or null if the AST failed to load. static std::unique_ptr<ASTUnit> - LoadFromASTFile(const std::string &Filename, - const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad, + LoadFromASTFile(StringRef Filename, const PCHContainerReader &PCHContainerRdr, + WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags, const FileSystemOptions &FileSystemOpts, std::shared_ptr<HeaderSearchOptions> HSOpts, diff --git a/clang/lib/CrossTU/CrossTranslationUnit.cpp b/clang/lib/CrossTU/CrossTranslationUnit.cpp index 986470042bd83c..9faf2a8a173411 100644 --- a/clang/lib/CrossTU/CrossTranslationUnit.cpp +++ b/clang/lib/CrossTU/CrossTranslationUnit.cpp @@ -566,9 +566,9 @@ CrossTranslationUnitContext::ASTLoader::loadFromDump(StringRef ASTDumpPath) { IntrusiveRefCntPtr<DiagnosticsEngine> Diags( new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient)); return ASTUnit::LoadFromASTFile( - std::string(ASTDumpPath.str()), - CI.getPCHContainerOperations()->getRawReader(), ASTUnit::LoadEverything, - Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOptsPtr()); + ASTDumpPath, CI.getPCHContainerOperations()->getRawReader(), + ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(), + CI.getHeaderSearchOptsPtr()); } /// Load the AST from a source-file, which is supposed to be located inside the diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 84e273a3949ef0..93836ec5402faa 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -802,7 +802,7 @@ void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags, } std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile( - const std::string &Filename, const PCHContainerReader &PCHContainerRdr, + StringRef Filename, const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad, IntrusiveRefCntPtr<DiagnosticsEngine> Diags, const FileSystemOptions &FileSystemOpts, std::shared_ptr<HeaderSearchOptions> HSOpts, diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp index a9c45e525c696c..81eea9c4c4dc58 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -625,8 +625,8 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, StringRef InputFile = Input.getFile(); std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile( - std::string(InputFile), CI.getPCHContainerReader(), - ASTUnit::LoadPreprocessorOnly, ASTDiags, CI.getFileSystemOpts(), + InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly, + ASTDiags, CI.getFileSystemOpts(), /*HeaderSearchOptions=*/nullptr); if (!AST) return false; @@ -693,9 +693,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, StringRef InputFile = Input.getFile(); std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile( - std::string(InputFile), CI.getPCHContainerReader(), - ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(), - CI.getHeaderSearchOptsPtr(), CI.getLangOptsPtr()); + InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags, + CI.getFileSystemOpts(), CI.getHeaderSearchOptsPtr(), + CI.getLangOptsPtr()); if (!AST) return false; diff --git a/clang/tools/c-index-test/core_main.cpp b/clang/tools/c-index-test/core_main.cpp index 003b1baef3a252..c43bff21962118 100644 --- a/clang/tools/c-index-test/core_main.cpp +++ b/clang/tools/c-index-test/core_main.cpp @@ -274,12 +274,12 @@ static bool printSourceSymbolsFromModule(StringRef modulePath, IntrusiveRefCntPtr<DiagnosticsEngine> Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions()); - std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( - std::string(modulePath), *pchRdr, ASTUnit::LoadASTOnly, Diags, - FileSystemOpts, HSOpts, /*LangOpts=*/nullptr, - /*OnlyLocalDecls=*/true, CaptureDiagsKind::None, - /*AllowASTWithCompilerErrors=*/true, - /*UserFilesAreVolatile=*/false); + std::unique_ptr<ASTUnit> AU = + ASTUnit::LoadFromASTFile(modulePath, *pchRdr, ASTUnit::LoadASTOnly, Diags, + FileSystemOpts, HSOpts, /*LangOpts=*/nullptr, + /*OnlyLocalDecls=*/true, CaptureDiagsKind::None, + /*AllowASTWithCompilerErrors=*/true, + /*UserFilesAreVolatile=*/false); if (!AU) { errs() << "failed to create TU for: " << modulePath << '\n'; return true; diff --git a/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp b/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp index c048f335f91ca1..3a2c32cfa4684b 100644 --- a/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp +++ b/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp @@ -155,7 +155,7 @@ static bool HandleAST(StringRef AstPath) { IntrusiveRefCntPtr<DiagnosticsEngine> DiagEngine = GetDiagnosticsEngine(); std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile( - AstPath.str(), CI->getPCHContainerOperations()->getRawReader(), + AstPath, CI->getPCHContainerOperations()->getRawReader(), ASTUnit::LoadASTOnly, DiagEngine, CI->getFileSystemOpts(), CI->getHeaderSearchOptsPtr()); diff --git a/clang/unittests/Frontend/ASTUnitTest.cpp b/clang/unittests/Frontend/ASTUnitTest.cpp index 30d2731897e7f3..19b5d9bb41466a 100644 --- a/clang/unittests/Frontend/ASTUnitTest.cpp +++ b/clang/unittests/Frontend/ASTUnitTest.cpp @@ -92,8 +92,8 @@ TEST_F(ASTUnitTest, SaveLoadPreservesLangOptionsInPrintingPolicy) { auto HSOpts = std::make_shared<HeaderSearchOptions>(); std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( - std::string(ASTFileName.str()), PCHContainerOps->getRawReader(), - ASTUnit::LoadEverything, Diags, FileSystemOptions(), HSOpts); + ASTFileName, PCHContainerOps->getRawReader(), ASTUnit::LoadEverything, + Diags, FileSystemOptions(), HSOpts); if (!AU) FAIL() << "failed to load ASTUnit"; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits