Skip to content

Conversation

@naveen-seth
Copy link
Contributor

This optimizes translateStandaloneDiag to avoid copying strings in multiple places and cleans up a hard-to-read transform.

…nostics (NFC)

This optimizes translateStandaloneDiag to avoid copying strings in
multiple places and cleans up a hard-to-read transform.
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Dec 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2025

@llvm/pr-subscribers-clang

Author: Naveen Seth Hanig (naveen-seth)

Changes

This optimizes translateStandaloneDiag to avoid copying strings in multiple places and cleans up a hard-to-read transform.


Full diff: https://github.com/llvm/llvm-project/pull/172746.diff

3 Files Affected:

  • (modified) clang/include/clang/Frontend/StandaloneDiagnostic.h (+1-1)
  • (modified) clang/lib/Frontend/ASTUnit.cpp (+5-7)
  • (modified) clang/lib/Frontend/StandaloneDiagnostic.cpp (+6-6)
diff --git a/clang/include/clang/Frontend/StandaloneDiagnostic.h b/clang/include/clang/Frontend/StandaloneDiagnostic.h
index c23d5f95e0c2f..d92d58c218c71 100644
--- a/clang/include/clang/Frontend/StandaloneDiagnostic.h
+++ b/clang/include/clang/Frontend/StandaloneDiagnostic.h
@@ -74,7 +74,7 @@ struct StandaloneDiagnostic {
 /// StandaloneDiagnostics themselfs cannot be emitted directly.
 StoredDiagnostic
 translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
-                        const StandaloneDiagnostic &StandaloneDiag,
+                        StandaloneDiagnostic StandaloneDiag,
                         llvm::StringMap<SourceLocation> &SrcLocCache);
 
 } // namespace clang
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index e72317da64596..89c4a9edc1051 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -1225,13 +1225,11 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
   if (SavedMainFileBuffer) {
     StoredDiagnostics.clear();
     StoredDiagnostics.reserve(PreambleDiagnostics.size());
-    llvm::transform(std::move(PreambleDiagnostics),
-                    std::back_inserter(StoredDiagnostics),
-                    [&](auto &&StandaloneDiag) {
-                      return translateStandaloneDiag(
-                          getFileManager(), getSourceManager(),
-                          std::move(StandaloneDiag), PreambleSrcLocCache);
-                    });
+    for (auto &PreambleDiag : PreambleDiagnostics) {
+      StoredDiagnostics.push_back(translateStandaloneDiag(
+          getFileManager(), getSourceManager(), std::move(PreambleDiag),
+          PreambleSrcLocCache));
+    }
   } else
     PreambleSrcLocCache.clear();
 
diff --git a/clang/lib/Frontend/StandaloneDiagnostic.cpp b/clang/lib/Frontend/StandaloneDiagnostic.cpp
index 4f19c91b7d266..d5776a5377bcf 100644
--- a/clang/lib/Frontend/StandaloneDiagnostic.cpp
+++ b/clang/lib/Frontend/StandaloneDiagnostic.cpp
@@ -55,7 +55,7 @@ StandaloneDiagnostic::StandaloneDiagnostic(const LangOptions &LangOpts,
 
 StoredDiagnostic
 translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
-                        const StandaloneDiagnostic &StandaloneDiag,
+                        StandaloneDiagnostic StandaloneDiag,
                         llvm::StringMap<SourceLocation> &SrcLocCache) {
   const auto FileRef = FileMgr.getOptionalFileRef(StandaloneDiag.Filename);
   if (!FileRef)
@@ -77,7 +77,7 @@ translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
 
     if (FileLoc.isInvalid())
       return StoredDiagnostic(StandaloneDiag.Level, StandaloneDiag.ID,
-                              StandaloneDiag.Message);
+                              std::move(StandaloneDiag.Message));
 
     SrcLocCache[StandaloneDiag.Filename] = FileLoc;
   }
@@ -100,9 +100,9 @@ translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
 
   SmallVector<FixItHint, 2> TranslatedFixIts;
   TranslatedFixIts.reserve(StandaloneDiag.FixIts.size());
-  for (const auto &FixIt : StandaloneDiag.FixIts) {
+  for (auto &FixIt : StandaloneDiag.FixIts) {
     FixItHint TranslatedFixIt;
-    TranslatedFixIt.CodeToInsert = FixIt.CodeToInsert;
+    TranslatedFixIt.CodeToInsert = std::move(FixIt.CodeToInsert);
     TranslatedFixIt.RemoveRange = ConvertOffsetRange(FixIt.RemoveRange);
     TranslatedFixIt.InsertFromRange = ConvertOffsetRange(FixIt.InsertFromRange);
     TranslatedFixIt.BeforePreviousInsertions = FixIt.BeforePreviousInsertions;
@@ -110,8 +110,8 @@ translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
   }
 
   return StoredDiagnostic(StandaloneDiag.Level, StandaloneDiag.ID,
-                          StandaloneDiag.Message, Loc, TranslatedRanges,
-                          TranslatedFixIts);
+                          std::move(StandaloneDiag.Message), Loc,
+                          TranslatedRanges, TranslatedFixIts);
 }
 
 } // namespace clang

@naveen-seth
Copy link
Contributor Author

For additional context:

PR #152770 (WIP) previously used a separate, slightly different implementation of StandaloneDiagnostic and related code from the one in ASTUnit.

After PR #169599 resolved the linkage issues and extracted StandaloneDiagnostic from the ASTUnit implementation, the implementation could be shared.

This change applies the optimizations from PR #152770’s translateStandaloneDiag implementation to the now-shared implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants