diff options
author | Nick Lewycky <nicholas@mxc.ca> | 2010-04-15 06:46:58 +0000 |
---|---|---|
committer | Nick Lewycky <nicholas@mxc.ca> | 2010-04-15 06:46:58 +0000 |
commit | d4a97a18ea3cda3ba095e7c0c6708e7a39cf31db (patch) | |
tree | 45ab7e6383f71ff83d4e424e9b888895c009882c /lib/Frontend/FrontendActions.cpp | |
parent | 264b7f26b872f4307c70cf6c68d84fdce620f5bb (diff) |
Teach -fixit to modify all of its inputs instead of just the main file, unless
-fixit-at specified a particular fixit to fix, or the -o flag was used.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101359 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Frontend/FrontendActions.cpp')
-rw-r--r-- | lib/Frontend/FrontendActions.cpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp index 6d7c6a8460..ce3e841b90 100644 --- a/lib/Frontend/FrontendActions.cpp +++ b/lib/Frontend/FrontendActions.cpp @@ -134,6 +134,23 @@ static bool AddFixItLocations(CompilerInstance &CI, FixItRewrite.addFixItLocation(Requested); } + const std::string &OutputFile = CI.getFrontendOpts().OutputFile; + if (Locs.empty() && !OutputFile.empty()) { + // FIXME: we will issue "FIX-IT applied suggested code changes" for every + // input, but only the main file will actually be rewritten. + const std::vector<std::pair<FrontendOptions::InputKind, std::string> > &Inputs = + CI.getFrontendOpts().Inputs; + for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { + const FileEntry *File = CI.getFileManager().getFile(Inputs[i].second); + assert(File && "Input file not found in FileManager"); + RequestedSourceLocation Requested; + Requested.File = File; + Requested.Line = 0; + Requested.Column = 0; + FixItRewrite.addFixItLocation(Requested); + } + } + return true; } @@ -149,7 +166,34 @@ bool FixItAction::BeginSourceFileAction(CompilerInstance &CI, void FixItAction::EndSourceFileAction() { const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); - Rewriter->WriteFixedFile(getCurrentFile(), FEOpts.OutputFile); + if (!FEOpts.OutputFile.empty()) { + // When called with 'clang -fixit -o filename' output only the main file. + + const SourceManager &SM = getCompilerInstance().getSourceManager(); + FileID MainFileID = SM.getMainFileID(); + if (!Rewriter->IsModified(MainFileID)) { + getCompilerInstance().getDiagnostics().Report( + diag::note_fixit_main_file_unchanged); + return; + } + + llvm::OwningPtr<llvm::raw_ostream> OwnedStream; + llvm::raw_ostream *OutFile; + if (FEOpts.OutputFile == "-") { + OutFile = &llvm::outs(); + } else { + std::string Err; + OutFile = new llvm::raw_fd_ostream(FEOpts.OutputFile.c_str(), Err, + llvm::raw_fd_ostream::F_Binary); + OwnedStream.reset(OutFile); + } + + Rewriter->WriteFixedFile(MainFileID, *OutFile); + return; + } + + // Otherwise rewrite all files. + Rewriter->WriteFixedFiles(); } ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, |