blob: 33ab0b7c99b30a241cb9992ef4d3995f555d1522 [file] [log] [blame]
Douglas Gregor558cb562009-04-02 01:08:08 +00001//===--- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This is a diagnostic client adaptor that performs rewrites as
11// suggested by code modification hints attached to diagnostics. It
12// then forwards any diagnostics to the adapted diagnostic client.
13//
14//===----------------------------------------------------------------------===//
15#include "clang/Basic/SourceManager.h"
16#include "clang/Frontend/FixItRewriter.h"
17#include "clang/Rewrite/Rewriter.h"
18#include "llvm/ADT/OwningPtr.h"
19#include "llvm/Support/Streams.h"
20#include "llvm/Support/raw_ostream.h"
21#include "llvm/System/Path.h"
22#include <cstdio>
23using namespace clang;
24
25FixItRewriter::FixItRewriter(DiagnosticClient *Client,
26 SourceManager &SourceMgr)
27 : Client(Client), NumFailures(0) {
28 Rewrite = new Rewriter(SourceMgr);
29}
30
31FixItRewriter::~FixItRewriter() {
32 delete Rewrite;
33}
34
35bool FixItRewriter::WriteFixedFile(const std::string &InFileName,
36 const std::string &OutFileName) {
37 if (NumFailures > 0) {
38 // FIXME: Use diagnostic machinery!
39 std::fprintf(stderr,
40 "%d fix-it failures detected; code will not be modified",
41 NumFailures);
42 return true;
43 }
44
45 llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
46 llvm::raw_ostream *OutFile;
47 if (OutFileName == "-") {
48 OutFile = &llvm::outs();
49 } else if (!OutFileName.empty()) {
50 std::string Err;
51 OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(),
52 // set binary mode (critical for Windoze)
53 true,
54 Err);
55 OwnedStream.reset(OutFile);
56 } else if (InFileName == "-") {
57 OutFile = &llvm::outs();
58 } else {
59 llvm::sys::Path Path(InFileName);
Douglas Gregor26103482009-04-02 03:14:12 +000060 std::string Suffix = Path.getSuffix();
Douglas Gregor558cb562009-04-02 01:08:08 +000061 Path.eraseSuffix();
Douglas Gregor26103482009-04-02 03:14:12 +000062 Path.appendSuffix("fixit." + Suffix);
Douglas Gregor558cb562009-04-02 01:08:08 +000063 std::string Err;
64 OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(),
65 // set binary mode (critical for Windoze)
66 true,
67 Err);
68 OwnedStream.reset(OutFile);
69 }
70
71 FileID MainFileID = Rewrite->getSourceMgr().getMainFileID();
72 if (const RewriteBuffer *RewriteBuf =
73 Rewrite->getRewriteBufferFor(MainFileID)) {
74 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
75 } else {
76 std::fprintf(stderr, "Main file is unchanged\n");
77 }
78 OutFile->flush();
79
80 return false;
81}
82
83bool FixItRewriter::IncludeInDiagnosticCounts() const {
84 return Client? Client->IncludeInDiagnosticCounts() : false;
85}
86
87void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
88 const DiagnosticInfo &Info) {
89 if (Client)
90 Client->HandleDiagnostic(DiagLevel, Info);
91
92 // Make sure that we can perform all of the modifications we
93 // in this diagnostic.
94 bool CanRewrite = true;
95 for (unsigned Idx = 0; Idx < Info.getNumCodeModificationHints(); ++Idx) {
96 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
97 if (Hint.RemoveRange.isValid() &&
98 (!Rewrite->isRewritable(Hint.RemoveRange.getBegin()) ||
99 !Rewrite->isRewritable(Hint.RemoveRange.getEnd()) ||
100 Rewrite->getRangeSize(Hint.RemoveRange) == -1)) {
101 CanRewrite = false;
102 break;
103 }
104
105 if (Hint.InsertionLoc.isValid() &&
106 !Rewrite->isRewritable(Hint.InsertionLoc)) {
107 CanRewrite = false;
108 break;
109 }
110 }
111
112 if (!CanRewrite) // FIXME: warn the user that this rewrite couldn't be done
113 return;
114
115 bool Failed = false;
116 for (unsigned Idx = 0; Idx < Info.getNumCodeModificationHints(); ++Idx) {
117 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
118 if (Hint.RemoveRange.isValid()) {
119 if (Hint.CodeToInsert.empty()) {
120 // We're removing code.
121 if (Rewrite->RemoveText(Hint.RemoveRange.getBegin(),
122 Rewrite->getRangeSize(Hint.RemoveRange)))
123 Failed = true;
124 } else {
125 // We're replacing code.
126 if (Rewrite->ReplaceText(Hint.RemoveRange.getBegin(),
127 Rewrite->getRangeSize(Hint.RemoveRange),
128 Hint.CodeToInsert.c_str(),
129 Hint.CodeToInsert.size()))
130 Failed = true;
131 }
132 } else {
133 // We're adding code.
134 if (Rewrite->InsertStrBefore(Hint.InsertionLoc, Hint.CodeToInsert))
135 Failed = true;
136 }
137 }
138
139 if (Failed)
140 ++NumFailures;
141}