Skip to content

Commit 7dd2dd8

Browse files
committed
Further cleanup
1 parent 1cac45f commit 7dd2dd8

File tree

3 files changed

+48
-40
lines changed

3 files changed

+48
-40
lines changed

clang/include/clang/Frontend/StandaloneDiagnostic.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@
1414
#ifndef LLVM_CLANG_FRONTEND_STANDALONEDIAGNOSTICS_H
1515
#define LLVM_CLANG_FRONTEND_STANDALONEDIAGNOSTICS_H
1616

17-
#include "clang/Basic/DiagnosticIDs.h"
18-
#include "clang/Basic/DiagnosticOptions.h"
19-
#include "clang/Basic/SourceLocation.h"
17+
#include "clang/Basic/Diagnostic.h"
2018
#include "clang/Basic/SourceManager.h"
21-
#include "clang/Basic/Specifiers.h"
22-
#include "llvm/ADT/StringExtras.h"
23-
#include <cassert>
2419
#include <string>
2520
#include <vector>
2621

@@ -67,15 +62,23 @@ struct StandaloneDiagnostic {
6762
std::vector<StandaloneFixIt> FixIts;
6863
};
6964

70-
/// Translates \c StandaloneDiag into a StoredDiagnostic, associating it with
71-
/// the provided FileManager and SourceManager.
65+
/// Translates all StandaloneDiagnostics in \p InDiags into StoredDiagnostics,
66+
/// associating them with the given FileManager and SourceManager.
7267
///
73-
/// This allows the diagnostic to be emitted using the diagnostics engine, since
74-
/// StandaloneDiagnostics themselfs cannot be emitted directly.
75-
StoredDiagnostic
76-
translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
77-
const StandaloneDiagnostic &StandaloneDiag,
78-
llvm::StringMap<SourceLocation> &SrcLocCache);
68+
/// This enables the diagnostics to be emitted through the diagnostics engine,
69+
/// since StandaloneDiagnostics cannot be emitted directly.
70+
///
71+
/// \param FileMgr The FileManager for the translated diagnostics.
72+
/// \param SrcMgr The SourceManager for the translated diagnostics.
73+
/// \param InDiags The input collection of StandaloneDiagnostics to translate.
74+
/// \param OutDiags The output list of resulting StoredDiagnostics.
75+
/// \param SrcLocCache Cache mapping filenames to source locations to speed up
76+
/// translation.
77+
void translateStandaloneDiagnostics(
78+
FileManager &FileMgr, SourceManager &SrcMgr,
79+
llvm::SmallVectorImpl<StandaloneDiagnostic> &&InDiags,
80+
llvm::SmallVectorImpl<StoredDiagnostic> &OutDiags,
81+
llvm::StringMap<SourceLocation> &SrcLocCache);
7982

8083
} // namespace clang
8184

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,17 +1222,11 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
12221222
if (!Act->BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]))
12231223
return true;
12241224

1225-
if (SavedMainFileBuffer) {
1226-
StoredDiagnostics.clear();
1227-
StoredDiagnostics.reserve(PreambleDiagnostics.size());
1228-
llvm::transform(std::move(PreambleDiagnostics),
1229-
std::back_inserter(StoredDiagnostics),
1230-
[&](auto &&StandaloneDiag) {
1231-
return translateStandaloneDiag(
1232-
getFileManager(), getSourceManager(),
1233-
std::move(StandaloneDiag), PreambleSrcLocCache);
1234-
});
1235-
} else
1225+
if (SavedMainFileBuffer)
1226+
translateStandaloneDiagnostics(getFileManager(), getSourceManager(),
1227+
std::move(PreambleDiagnostics),
1228+
StoredDiagnostics, PreambleSrcLocCache);
1229+
else
12361230
PreambleSrcLocCache.clear();
12371231

12381232
if (llvm::Error Err = Act->Execute()) {

clang/lib/Frontend/StandaloneDiagnostic.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ StandaloneDiagnostic::StandaloneDiagnostic(const LangOptions &LangOpts,
5353
FixIts.emplace_back(SrcMgr, LangOpts, FixIt);
5454
}
5555

56-
StoredDiagnostic
57-
translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
58-
const StandaloneDiagnostic &StandaloneDiag,
59-
llvm::StringMap<SourceLocation> &SrcLocCache) {
56+
static StoredDiagnostic
57+
translateStandaloneDiagnostic(FileManager &FileMgr, SourceManager &SrcMgr,
58+
StandaloneDiagnostic &&StandaloneDiag,
59+
llvm::StringMap<SourceLocation> &SrcLocCache) {
6060
const auto FileRef = FileMgr.getOptionalFileRef(StandaloneDiag.Filename);
6161
if (!FileRef)
6262
return StoredDiagnostic(StandaloneDiag.Level, StandaloneDiag.ID,
63-
StandaloneDiag.Message);
63+
std::move(StandaloneDiag.Message));
6464

6565
// Try to get FileLoc from cache first
6666
SourceLocation FileLoc;
67-
auto It = SrcLocCache.find(StandaloneDiag.Filename);
68-
if (It != SrcLocCache.end()) {
67+
if (const auto It = SrcLocCache.find(StandaloneDiag.Filename);
68+
It != SrcLocCache.end()) {
6969
FileLoc = It->getValue();
7070
}
7171

@@ -95,23 +95,34 @@ translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
9595

9696
SmallVector<CharSourceRange, 4> TranslatedRanges;
9797
TranslatedRanges.reserve(StandaloneDiag.Ranges.size());
98-
transform(StandaloneDiag.Ranges, std::back_inserter(TranslatedRanges),
99-
ConvertOffsetRange);
98+
for (const auto &Range : StandaloneDiag.Ranges)
99+
TranslatedRanges.push_back(ConvertOffsetRange(Range));
100100

101101
SmallVector<FixItHint, 2> TranslatedFixIts;
102102
TranslatedFixIts.reserve(StandaloneDiag.FixIts.size());
103-
for (const auto &FixIt : StandaloneDiag.FixIts) {
104-
FixItHint TranslatedFixIt;
105-
TranslatedFixIt.CodeToInsert = FixIt.CodeToInsert;
103+
for (auto &FixIt : StandaloneDiag.FixIts) {
104+
auto &TranslatedFixIt = TranslatedFixIts.emplace_back();
105+
TranslatedFixIt.CodeToInsert = std::move(FixIt.CodeToInsert);
106106
TranslatedFixIt.RemoveRange = ConvertOffsetRange(FixIt.RemoveRange);
107107
TranslatedFixIt.InsertFromRange = ConvertOffsetRange(FixIt.InsertFromRange);
108108
TranslatedFixIt.BeforePreviousInsertions = FixIt.BeforePreviousInsertions;
109-
TranslatedFixIts.push_back(std::move(TranslatedFixIt));
110109
}
111110

112111
return StoredDiagnostic(StandaloneDiag.Level, StandaloneDiag.ID,
113-
StandaloneDiag.Message, Loc, TranslatedRanges,
114-
TranslatedFixIts);
112+
std::move(StandaloneDiag.Message), Loc,
113+
TranslatedRanges, TranslatedFixIts);
114+
}
115+
116+
void translateStandaloneDiagnostics(
117+
FileManager &FileMgr, SourceManager &SrcMgr,
118+
llvm::SmallVectorImpl<StandaloneDiagnostic> &&InDiags,
119+
llvm::SmallVectorImpl<StoredDiagnostic> &OutDiags,
120+
llvm::StringMap<SourceLocation> &SrcLocCache) {
121+
InDiags.clear();
122+
InDiags.reserve(OutDiags.size());
123+
for (auto &InDiag : InDiags)
124+
OutDiags.push_back(translateStandaloneDiagnostic(
125+
FileMgr, SrcMgr, std::move(InDiag), SrcLocCache));
115126
}
116127

117128
} // namespace clang

0 commit comments

Comments
 (0)