@@ -51,27 +51,32 @@ LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
51
51
52
52
namespace {
53
53
54
- // / DeserializedDeclsLineRangePrinter dumps ranges of deserialized declarations to aid debugging and bug minimization.
55
- // / It implements ASTConsumer and ASTDeserializationListener, so that an object of DeserializedDeclsLineRangePrinter registers
56
- // / as its own listener.
57
- // / The ASTDeserializationListener interface provides the DeclRead callback that we use to collect the deserialized Decls.
58
- // / Note that printing or otherwise processing them as this point is dangerous, since that could trigger additional
59
- // / deserialization and crash compilation.
60
- // / Therefore, we process the collected Decls in HandleTranslationUnit method of ASTConsumer.
61
- // / This is a safe point, since we know that by this point all the Decls needed by the compiler frontend have been
62
- // / deserialized. In case our processing causes further deserialization, DeclRead from the listener might be called again.
54
+ // / DeserializedDeclsLineRangePrinter dumps ranges of deserialized declarations
55
+ // / to aid debugging and bug minimization. It implements ASTConsumer and
56
+ // / ASTDeserializationListener, so that an object of
57
+ // / DeserializedDeclsLineRangePrinter registers as its own listener. The
58
+ // / ASTDeserializationListener interface provides the DeclRead callback that we
59
+ // / use to collect the deserialized Decls. Note that printing or otherwise
60
+ // / processing them as this point is dangerous, since that could trigger
61
+ // / additional deserialization and crash compilation. Therefore, we process the
62
+ // / collected Decls in HandleTranslationUnit method of ASTConsumer. This is a
63
+ // / safe point, since we know that by this point all the Decls needed by the
64
+ // / compiler frontend have been deserialized. In case our processing causes
65
+ // / further deserialization, DeclRead from the listener might be called again.
63
66
// / However, at that point we don't accept any more Decls for processing.
64
- class DeserializedDeclsLineRangePrinter : public ASTDeserializationListener , public ASTConsumer {
67
+ class DeserializedDeclsLineRangePrinter : public ASTDeserializationListener ,
68
+ public ASTConsumer {
65
69
public:
66
- explicit DeserializedDeclsLineRangePrinter (SourceManager &SM, std::unique_ptr<llvm::raw_fd_ostream> OS)
70
+ explicit DeserializedDeclsLineRangePrinter (
71
+ SourceManager &SM, std::unique_ptr<llvm::raw_fd_ostream> OS)
67
72
: ASTDeserializationListener(), SM(SM), OS(std::move(OS)) {}
68
73
69
74
void DeclRead (GlobalDeclID ID, const Decl *D) override {
70
75
if (!IsCollectingDecls) {
71
76
return ;
72
77
}
73
78
if (!D || isa<TranslationUnitDecl>(D) || isa<LinkageSpecDecl>(D) ||
74
- isa<NamespaceDecl>(D))
79
+ isa<NamespaceDecl>(D))
75
80
return ;
76
81
if (auto *DC = D->getDeclContext (); !DC || !DC->isFileContext ())
77
82
return ;
@@ -81,15 +86,15 @@ class DeserializedDeclsLineRangePrinter : public ASTDeserializationListener, pub
81
86
82
87
using Position = std::pair<unsigned , unsigned >;
83
88
struct RequiredRanges {
84
- StringRef Filename;
85
- std::vector<std::pair<Position, Position>> FromTo;
89
+ StringRef Filename;
90
+ std::vector<std::pair<Position, Position>> FromTo;
86
91
};
87
92
void HandleTranslationUnit (ASTContext &Context) override {
88
93
IsCollectingDecls = false ;
89
94
std::vector<const Decl *> Decls = std::move (PendingDecls);
90
95
if (!PendingDecls.empty ()) {
91
96
llvm::errs () << " Deserialized more decls while printing, total of "
92
- << PendingDecls.size () << " \n " ;
97
+ << PendingDecls.size () << " \n " ;
93
98
PendingDecls.clear ();
94
99
}
95
100
@@ -112,18 +117,20 @@ class DeserializedDeclsLineRangePrinter : public ASTDeserializationListener, pub
112
117
113
118
auto &Data = FileToLines[F];
114
119
if (!Data.Ref )
115
- Data.Ref =
116
- SM.getFileEntryRefForID (SM.getFileID (R.getBegin ()));
117
- Data.FromTo .push_back ({{SM.getSpellingLineNumber (R.getBegin ()), SM.getSpellingColumnNumber (R.getBegin ())},
118
- {SM.getSpellingLineNumber (R.getEnd ()), SM.getSpellingColumnNumber (R.getEnd ())}});
120
+ Data.Ref = SM.getFileEntryRefForID (SM.getFileID (R.getBegin ()));
121
+ Data.FromTo .push_back ({{SM.getSpellingLineNumber (R.getBegin ()),
122
+ SM.getSpellingColumnNumber (R.getBegin ())},
123
+ {SM.getSpellingLineNumber (R.getEnd ()),
124
+ SM.getSpellingColumnNumber (R.getEnd ())}});
119
125
}
120
126
121
127
std::vector<RequiredRanges> Result;
122
128
for (auto &[F, Data] : FileToLines) {
123
- auto & FromTo = Data.FromTo ;
129
+ auto & FromTo = Data.FromTo ;
124
130
assert (!FromTo.empty ());
125
131
126
- if (!Data.Ref ) continue ;
132
+ if (!Data.Ref )
133
+ continue ;
127
134
128
135
llvm::sort (FromTo);
129
136
@@ -142,7 +149,7 @@ class DeserializedDeclsLineRangePrinter : public ASTDeserializationListener, pub
142
149
printJson (Result);
143
150
}
144
151
145
- void printJson (const std::vector<RequiredRanges>& Result) {
152
+ void printJson (const std::vector<RequiredRanges> & Result) {
146
153
*OS << " {\n " ;
147
154
*OS << " \" required_ranges\" : [\n " ;
148
155
for (size_t i = 0 ; i < Result.size (); ++i) {
@@ -182,13 +189,12 @@ class DeserializedDeclsLineRangePrinter : public ASTDeserializationListener, pub
182
189
}
183
190
184
191
private:
185
- std::vector<const Decl *> PendingDecls;
186
- bool IsCollectingDecls = true ;
187
- const SourceManager &SM;
188
- std::unique_ptr<llvm::raw_ostream> OS;
192
+ std::vector<const Decl *> PendingDecls;
193
+ bool IsCollectingDecls = true ;
194
+ const SourceManager &SM;
195
+ std::unique_ptr<llvm::raw_ostream> OS;
189
196
};
190
197
191
-
192
198
// / Dumps deserialized declarations.
193
199
class DeserializedDeclsDumper : public DelegatingDeserializationListener {
194
200
public:
@@ -262,15 +268,22 @@ FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
262
268
return nullptr ;
263
269
264
270
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
265
- llvm::StringRef DumpDeserializedDeclarationRangesPath = CI.getFrontendOpts ().DumpDeserializedDeclarationRangesPath ;
271
+ llvm::StringRef DumpDeserializedDeclarationRangesPath =
272
+ CI.getFrontendOpts ().DumpDeserializedDeclarationRangesPath ;
266
273
if (!DumpDeserializedDeclarationRangesPath.empty ()) {
267
274
std::error_code ErrorCode;
268
- auto FileStream = std::make_unique<llvm::raw_fd_ostream>(DumpDeserializedDeclarationRangesPath, ErrorCode, llvm::sys::fs::OF_None);
275
+ auto FileStream = std::make_unique<llvm::raw_fd_ostream>(
276
+ DumpDeserializedDeclarationRangesPath, ErrorCode,
277
+ llvm::sys::fs::OF_None);
269
278
if (!ErrorCode) {
270
- auto Printer = std::make_unique<DeserializedDeclsLineRangePrinter>(CI.getSourceManager (), std::move (FileStream));
279
+ auto Printer = std::make_unique<DeserializedDeclsLineRangePrinter>(
280
+ CI.getSourceManager (), std::move (FileStream));
271
281
Consumers.push_back (std::move (Printer));
272
282
} else {
273
- llvm::errs () << " Failed to create output file for -dump-deserialized-declaration-ranges flag, file path: " << DumpDeserializedDeclarationRangesPath << " , error: " << ErrorCode.message () << " \n " ;
283
+ llvm::errs () << " Failed to create output file for "
284
+ " -dump-deserialized-declaration-ranges flag, file path: "
285
+ << DumpDeserializedDeclarationRangesPath
286
+ << " , error: " << ErrorCode.message () << " \n " ;
274
287
}
275
288
}
276
289
@@ -340,7 +353,7 @@ FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
340
353
}
341
354
342
355
assert (Consumers.size () >= 1 && " should have added the main consumer" );
343
- if (Consumers.size () == 1 )
356
+ if (Consumers.size () == 1 )
344
357
return std::move (Consumers.front ());
345
358
return std::make_unique<MultiplexConsumer>(std::move (Consumers));
346
359
}
0 commit comments