diff options
Diffstat (limited to 'lib/Frontend/FixItRewriter.cpp')
-rw-r--r-- | lib/Frontend/FixItRewriter.cpp | 105 |
1 files changed, 37 insertions, 68 deletions
diff --git a/lib/Frontend/FixItRewriter.cpp b/lib/Frontend/FixItRewriter.cpp index 20d452e..7c9a566 100644 --- a/lib/Frontend/FixItRewriter.cpp +++ b/lib/Frontend/FixItRewriter.cpp @@ -14,6 +14,8 @@ //===----------------------------------------------------------------------===// #include "clang/Frontend/FixItRewriter.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" #include "clang/Frontend/FrontendDiagnostic.h" #include "llvm/Support/raw_ostream.h" @@ -24,8 +26,12 @@ using namespace clang; FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr, - const LangOptions &LangOpts) - : Diags(Diags), Rewrite(SourceMgr, LangOpts), NumFailures(0) { + const LangOptions &LangOpts, + FixItPathRewriter *PathRewriter) + : Diags(Diags), + Rewrite(SourceMgr, LangOpts), + PathRewriter(PathRewriter), + NumFailures(0) { Client = Diags.getClient(); Diags.setClient(this); } @@ -34,92 +40,53 @@ FixItRewriter::~FixItRewriter() { Diags.setClient(Client); } -bool FixItRewriter::WriteFixedFile(const std::string &InFileName, - const std::string &OutFileName) { +bool FixItRewriter::WriteFixedFile(FileID ID, llvm::raw_ostream &OS) { + const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID); + if (!RewriteBuf) return true; + RewriteBuf->write(OS); + OS.flush(); + return false; +} + +bool FixItRewriter::WriteFixedFiles() { if (NumFailures > 0) { Diag(FullSourceLoc(), diag::warn_fixit_no_changes); return true; } - llvm::OwningPtr<llvm::raw_ostream> OwnedStream; - llvm::raw_ostream *OutFile; - if (!OutFileName.empty()) { - std::string Err; - OutFile = new llvm::raw_fd_ostream(OutFileName.c_str(), Err, - llvm::raw_fd_ostream::F_Binary); - OwnedStream.reset(OutFile); - } else if (InFileName == "-") { - OutFile = &llvm::outs(); - } else { - llvm::sys::Path Path(InFileName); - std::string Suffix = Path.getSuffix(); - Path.eraseSuffix(); - Path.appendSuffix("fixit." + Suffix); + for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) { + const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first); + std::string Filename = Entry->getName(); + if (PathRewriter) + Filename = PathRewriter->RewriteFilename(Filename); std::string Err; - OutFile = new llvm::raw_fd_ostream(Path.c_str(), Err, - llvm::raw_fd_ostream::F_Binary); - OwnedStream.reset(OutFile); - } - - FileID MainFileID = Rewrite.getSourceMgr().getMainFileID(); - if (const RewriteBuffer *RewriteBuf = - Rewrite.getRewriteBufferFor(MainFileID)) { - *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); - } else { - Diag(FullSourceLoc(), diag::note_fixit_main_file_unchanged); + llvm::raw_fd_ostream OS(Filename.c_str(), Err, + llvm::raw_fd_ostream::F_Binary); + if (!Err.empty()) { + Diags.Report(clang::diag::err_fe_unable_to_open_output) + << Filename << Err; + continue; + } + RewriteBuffer &RewriteBuf = I->second; + RewriteBuf.write(OS); + OS.flush(); } - OutFile->flush(); return false; } bool FixItRewriter::IncludeInDiagnosticCounts() const { - return Client? Client->IncludeInDiagnosticCounts() : true; + return Client ? Client->IncludeInDiagnosticCounts() : true; } void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel, const DiagnosticInfo &Info) { Client->HandleDiagnostic(DiagLevel, Info); - // Skip over any diagnostics that are ignored. - if (DiagLevel == Diagnostic::Ignored) + // Skip over any diagnostics that are ignored or notes. + if (DiagLevel <= Diagnostic::Note) return; - if (!FixItLocations.empty()) { - // The user has specified the locations where we should perform - // the various fix-it modifications. - - // If this diagnostic does not have any code modifications, - // completely ignore it, even if it's an error: fix-it locations - // are meant to perform specific fix-ups even in the presence of - // other errors. - if (Info.getNumFixItHints() == 0) - return; - - // See if the location of the error is one that matches what the - // user requested. - bool AcceptableLocation = false; - const FileEntry *File - = Rewrite.getSourceMgr().getFileEntryForID( - Info.getLocation().getFileID()); - unsigned Line = Info.getLocation().getSpellingLineNumber(); - unsigned Column = Info.getLocation().getSpellingColumnNumber(); - for (llvm::SmallVector<RequestedSourceLocation, 4>::iterator - Loc = FixItLocations.begin(), LocEnd = FixItLocations.end(); - Loc != LocEnd; ++Loc) { - if (Loc->File == File && Loc->Line == Line && Loc->Column == Column) { - AcceptableLocation = true; - break; - } - } - - if (!AcceptableLocation) - return; - } else if (DiagLevel == Diagnostic::Note) { - // Don't apply fix-it modifications in notes. - return; - } - // Make sure that we can perform all of the modifications we // in this diagnostic. bool CanRewrite = Info.getNumFixItHints() > 0; @@ -196,3 +163,5 @@ void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) { Diags.Report(Loc, DiagID); Diags.setClient(this); } + +FixItPathRewriter::~FixItPathRewriter() {} |