Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Frontend/Rewrite/FixItRewriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This is a diagnostic client adaptor that performs rewrites as
10
// suggested by code modification hints attached to diagnostics. It
11
// then forwards any diagnostics to the adapted diagnostic client.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/Rewrite/Frontend/FixItRewriter.h"
16
#include "clang/Basic/Diagnostic.h"
17
#include "clang/Basic/FileManager.h"
18
#include "clang/Basic/LLVM.h"
19
#include "clang/Basic/SourceLocation.h"
20
#include "clang/Basic/SourceManager.h"
21
#include "clang/Edit/Commit.h"
22
#include "clang/Edit/EditsReceiver.h"
23
#include "clang/Frontend/FrontendDiagnostic.h"
24
#include "clang/Rewrite/Core/RewriteBuffer.h"
25
#include "clang/Rewrite/Core/Rewriter.h"
26
#include "llvm/ADT/StringRef.h"
27
#include "llvm/Support/FileSystem.h"
28
#include "llvm/Support/raw_ostream.h"
29
#include <cstdio>
30
#include <memory>
31
#include <string>
32
#include <system_error>
33
#include <utility>
34
35
using namespace clang;
36
37
FixItRewriter::FixItRewriter(DiagnosticsEngine &Diags, SourceManager &SourceMgr,
38
                             const LangOptions &LangOpts,
39
                             FixItOptions *FixItOpts)
40
    : Diags(Diags), Editor(SourceMgr, LangOpts), Rewrite(SourceMgr, LangOpts),
41
70
      FixItOpts(FixItOpts) {
42
70
  Owner = Diags.takeClient();
43
70
  Client = Diags.getClient();
44
70
  Diags.setClient(this, false);
45
70
}
46
47
70
FixItRewriter::~FixItRewriter() {
48
70
  Diags.setClient(Client, Owner.release() != nullptr);
49
70
}
50
51
0
bool FixItRewriter::WriteFixedFile(FileID ID, raw_ostream &OS) {
52
0
  const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID);
53
0
  if (!RewriteBuf) return true;
54
0
  RewriteBuf->write(OS);
55
0
  OS.flush();
56
0
  return false;
57
0
}
58
59
namespace {
60
61
class RewritesReceiver : public edit::EditsReceiver {
62
  Rewriter &Rewrite;
63
64
public:
65
68
  RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) {}
66
67
299
  void insert(SourceLocation loc, StringRef text) override {
68
299
    Rewrite.InsertText(loc, text);
69
299
  }
70
71
578
  void replace(CharSourceRange range, StringRef text) override {
72
578
    Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
73
578
  }
74
};
75
76
} // namespace
77
78
bool FixItRewriter::WriteFixedFiles(
79
70
             std::vector<std::pair<std::string, std::string>> *RewrittenFiles) {
80
70
  if (NumFailures > 0 && 
!FixItOpts->FixWhatYouCan2
) {
81
2
    Diag(FullSourceLoc(), diag::warn_fixit_no_changes);
82
2
    return true;
83
2
  }
84
68
85
68
  RewritesReceiver Rec(Rewrite);
86
68
  Editor.applyRewrites(Rec);
87
68
88
68
  if (FixItOpts->InPlace) {
89
61
    // Overwriting open files on Windows is tricky, but the rewriter can do it
90
61
    // for us.
91
61
    Rewrite.overwriteChangedFiles();
92
61
    return false;
93
61
  }
94
7
95
14
  
for (iterator I = buffer_begin(), E = buffer_end(); 7
I != E;
++I7
) {
96
7
    const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
97
7
    int fd;
98
7
    std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
99
7
    std::error_code EC;
100
7
    std::unique_ptr<llvm::raw_fd_ostream> OS;
101
7
    if (fd != -1) {
102
2
      OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
103
5
    } else {
104
5
      OS.reset(new llvm::raw_fd_ostream(Filename, EC, llvm::sys::fs::F_None));
105
5
    }
106
7
    if (EC) {
107
0
      Diags.Report(clang::diag::err_fe_unable_to_open_output) << Filename
108
0
                                                              << EC.message();
109
0
      continue;
110
0
    }
111
7
    RewriteBuffer &RewriteBuf = I->second;
112
7
    RewriteBuf.write(*OS);
113
7
    OS->flush();
114
7
115
7
    if (RewrittenFiles)
116
2
      RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));
117
7
  }
118
7
119
7
  return false;
120
7
}
121
122
2.35k
bool FixItRewriter::IncludeInDiagnosticCounts() const {
123
2.35k
  return Client ? Client->IncludeInDiagnosticCounts() : 
true0
;
124
2.35k
}
125
126
void FixItRewriter::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
127
944
                                     const Diagnostic &Info) {
128
944
  // Default implementation (Warnings/errors count).
129
944
  DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
130
944
131
944
  if (!FixItOpts->Silent ||
132
944
      
DiagLevel >= DiagnosticsEngine::Error12
||
133
944
      
(0
DiagLevel == DiagnosticsEngine::Note0
&&
!PrevDiagSilenced0
) ||
134
944
      
(0
DiagLevel > DiagnosticsEngine::Note0
&&
Info.getNumFixItHints()0
)) {
135
944
    Client->HandleDiagnostic(DiagLevel, Info);
136
944
    PrevDiagSilenced = false;
137
944
  } else {
138
0
    PrevDiagSilenced = true;
139
0
  }
140
944
141
944
  // Skip over any diagnostics that are ignored or notes.
142
944
  if (DiagLevel <= DiagnosticsEngine::Note)
143
136
    return;
144
808
  // Skip over errors if we are only fixing warnings.
145
808
  if (DiagLevel >= DiagnosticsEngine::Error && 
FixItOpts->FixOnlyWarnings477
) {
146
1
    ++NumFailures;
147
1
    return;
148
1
  }
149
807
150
807
  // Make sure that we can perform all of the modifications we
151
807
  // in this diagnostic.
152
807
  edit::Commit commit(Editor);
153
807
  for (unsigned Idx = 0, Last = Info.getNumFixItHints();
154
1.72k
       Idx < Last; 
++Idx920
) {
155
920
    const FixItHint &Hint = Info.getFixItHint(Idx);
156
920
157
920
    if (Hint.CodeToInsert.empty()) {
158
220
      if (Hint.InsertFromRange.isValid())
159
26
        commit.insertFromRange(Hint.RemoveRange.getBegin(),
160
26
                           Hint.InsertFromRange, /*afterToken=*/false,
161
26
                           Hint.BeforePreviousInsertions);
162
194
      else
163
194
        commit.remove(Hint.RemoveRange);
164
700
    } else {
165
700
      if (Hint.RemoveRange.isTokenRange() ||
166
700
          
Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd()456
)
167
398
        commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
168
302
      else
169
302
        commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
170
302
                    /*afterToken=*/false, Hint.BeforePreviousInsertions);
171
700
    }
172
920
  }
173
807
  bool CanRewrite = Info.getNumFixItHints() > 0 && 
commit.isCommitable()763
;
174
807
175
807
  if (!CanRewrite) {
176
44
    if (Info.getNumFixItHints() > 0)
177
0
      Diag(Info.getLocation(), diag::note_fixit_in_macro);
178
44
179
44
    // If this was an error, refuse to perform any rewriting.
180
44
    if (DiagLevel >= DiagnosticsEngine::Error) {
181
2
      if (++NumFailures == 1)
182
1
        Diag(Info.getLocation(), diag::note_fixit_unfixed_error);
183
2
    }
184
44
    return;
185
44
  }
186
763
187
763
  if (!Editor.commit(commit)) {
188
0
    ++NumFailures;
189
0
    Diag(Info.getLocation(), diag::note_fixit_failed);
190
0
    return;
191
0
  }
192
763
193
763
  Diag(Info.getLocation(), diag::note_fixit_applied);
194
763
}
195
196
/// Emit a diagnostic via the adapted diagnostic client.
197
766
void FixItRewriter::Diag(SourceLocation Loc, unsigned DiagID) {
198
766
  // When producing this diagnostic, we temporarily bypass ourselves,
199
766
  // clear out any current diagnostic, and let the downstream client
200
766
  // format the diagnostic.
201
766
  Diags.setClient(Client, false);
202
766
  Diags.Clear();
203
766
  Diags.Report(Loc, DiagID);
204
766
  Diags.setClient(this, false);
205
766
}
206
207
70
FixItOptions::~FixItOptions() = default;