@@ -120,12 +120,11 @@ void rewriteSourceRange(Rewriter &R, const CharSourceRange &Range,
120
120
}
121
121
}
122
122
123
- static void emit (Rewriter &R, ASTContext &C) {
123
+ static void emit (Rewriter &R, ASTContext &C, bool &StdoutModeEmittedMainFile ) {
124
124
if (Verbose)
125
125
errs () << " Writing files out\n " ;
126
126
127
127
bool StdoutMode = (OutputPostfix == " -" && OutputDir.empty ());
128
- bool StdoutModeSawMainFile = false ;
129
128
SourceManager &SM = C.getSourceManager ();
130
129
// Iterate over each modified rewrite buffer.
131
130
for (auto Buffer = R.buffer_begin (); Buffer != R.buffer_end (); ++Buffer) {
@@ -226,9 +225,15 @@ static void emit(Rewriter &R, ASTContext &C) {
226
225
227
226
if (StdoutMode) {
228
227
if (Buffer->first == SM.getMainFileID ()) {
229
- // This is the new version of the main file. Print it to stdout.
230
- Buffer->second .write (outs ());
231
- StdoutModeSawMainFile = true ;
228
+ // This is the new version of the main file. Print it to stdout,
229
+ // except in the edge case where we have a compilation database with
230
+ // multiple translation units with the same main file and we already
231
+ // emitted a copy of the main file for a previous translation unit
232
+ // (https://github.com/correctcomputation/checkedc-clang/issues/374#issuecomment-893612654).
233
+ if (!StdoutModeEmittedMainFile) {
234
+ Buffer->second .write (outs ());
235
+ StdoutModeEmittedMainFile = true ;
236
+ }
232
237
} else {
233
238
unsigned ID = DE.getCustomDiagID (
234
239
UnwritableChangeDiagnosticLevel,
@@ -300,9 +305,10 @@ static void emit(Rewriter &R, ASTContext &C) {
300
305
}
301
306
}
302
307
303
- if (StdoutMode && !StdoutModeSawMainFile ) {
308
+ if (StdoutMode && !StdoutModeEmittedMainFile ) {
304
309
// The main file is unchanged. Write out its original content.
305
310
outs () << SM.getBufferOrFake (SM.getMainFileID ()).getBuffer ();
311
+ StdoutModeEmittedMainFile = true ;
306
312
}
307
313
}
308
314
@@ -632,7 +638,7 @@ void RewriteConsumer::HandleTranslationUnit(ASTContext &Context) {
632
638
}
633
639
634
640
// Output files.
635
- emit (R, Context);
641
+ emit (R, Context, StdoutModeEmittedMainFile );
636
642
637
643
Info.getPerfStats ().endRewritingTime ();
638
644
0 commit comments