blob: a53d1e56d14c1ce8e8fb82b12dd9ed9bfb6b9259 [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);
60 Path.eraseSuffix();
61 Path.appendSuffix("cpp");
62 std::string Err;
63 OutFile = new llvm::raw_fd_ostream(Path.toString().c_str(),
64 // set binary mode (critical for Windoze)
65 true,
66 Err);
67 OwnedStream.reset(OutFile);
68 }
69
70 FileID MainFileID = Rewrite->getSourceMgr().getMainFileID();
71 if (const RewriteBuffer *RewriteBuf =
72 Rewrite->getRewriteBufferFor(MainFileID)) {
73 *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end());
74 } else {
75 std::fprintf(stderr, "Main file is unchanged\n");
76 }
77 OutFile->flush();
78
79 return false;
80}
81
82bool FixItRewriter::IncludeInDiagnosticCounts() const {
83 return Client? Client->IncludeInDiagnosticCounts() : false;
84}
85
86void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel,
87 const DiagnosticInfo &Info) {
88 if (Client)
89 Client->HandleDiagnostic(DiagLevel, Info);
90
91 // Make sure that we can perform all of the modifications we
92 // in this diagnostic.
93 bool CanRewrite = true;
94 for (unsigned Idx = 0; Idx < Info.getNumCodeModificationHints(); ++Idx) {
95 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
96 if (Hint.RemoveRange.isValid() &&
97 (!Rewrite->isRewritable(Hint.RemoveRange.getBegin()) ||
98 !Rewrite->isRewritable(Hint.RemoveRange.getEnd()) ||
99 Rewrite->getRangeSize(Hint.RemoveRange) == -1)) {
100 CanRewrite = false;
101 break;
102 }
103
104 if (Hint.InsertionLoc.isValid() &&
105 !Rewrite->isRewritable(Hint.InsertionLoc)) {
106 CanRewrite = false;
107 break;
108 }
109 }
110
111 if (!CanRewrite) // FIXME: warn the user that this rewrite couldn't be done
112 return;
113
114 bool Failed = false;
115 for (unsigned Idx = 0; Idx < Info.getNumCodeModificationHints(); ++Idx) {
116 const CodeModificationHint &Hint = Info.getCodeModificationHint(Idx);
117 if (Hint.RemoveRange.isValid()) {
118 if (Hint.CodeToInsert.empty()) {
119 // We're removing code.
120 if (Rewrite->RemoveText(Hint.RemoveRange.getBegin(),
121 Rewrite->getRangeSize(Hint.RemoveRange)))
122 Failed = true;
123 } else {
124 // We're replacing code.
125 if (Rewrite->ReplaceText(Hint.RemoveRange.getBegin(),
126 Rewrite->getRangeSize(Hint.RemoveRange),
127 Hint.CodeToInsert.c_str(),
128 Hint.CodeToInsert.size()))
129 Failed = true;
130 }
131 } else {
132 // We're adding code.
133 if (Rewrite->InsertStrBefore(Hint.InsertionLoc, Hint.CodeToInsert))
134 Failed = true;
135 }
136 }
137
138 if (Failed)
139 ++NumFailures;
140}