<div class="gmail_quote">On Wed, May 23, 2012 at 12:57 AM, NAKAMURA Takumi <span dir="ltr"><<a href="mailto:geek4civic@gmail.com" target="_blank">geek4civic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<p>Manuel, I don't think it is compatible to Win32.<br>
Manipulating opened files would be hard on Win32.</p></blockquote><div><br></div><div>I cargo culted the general concept here from other parts of clang. Which parts exactly don't work with Win32?</div><div><br></div>
<div>Thanks,</div><div>/Manuel</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="gmail_quote">2012/05/23 2:05 "Manuel Klimek" <<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>>:<div><div class="h5"><br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: klimek<br>
Date: Tue May 22 12:01:35 2012<br>
New Revision: 157260<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=157260&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=157260&view=rev</a><br>
Log:<br>
Adds a method overwriteChangedFiles to the Rewriter. This is implemented by<br>
first writing the changed files to a temporary location and then overwriting<br>
the original files atomically.<br>
<br>
Also adds a RewriterTestContext to aid unit testing rewrting logic in general.<br>
<br>
<br>
Added:<br>
cfe/trunk/unittests/Tooling/RewriterTest.cpp (with props)<br>
cfe/trunk/unittests/Tooling/RewriterTestContext.h (with props)<br>
Modified:<br>
cfe/trunk/include/clang/Rewrite/Rewriter.h<br>
cfe/trunk/lib/Rewrite/Rewriter.cpp<br>
cfe/trunk/unittests/CMakeLists.txt<br>
<br>
Modified: cfe/trunk/include/clang/Rewrite/Rewriter.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/Rewriter.h?rev=157260&r1=157259&r2=157260&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/Rewriter.h?rev=157260&r1=157259&r2=157260&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Rewrite/Rewriter.h (original)<br>
+++ cfe/trunk/include/clang/Rewrite/Rewriter.h Tue May 22 12:01:35 2012<br>
@@ -279,6 +279,13 @@<br>
buffer_iterator buffer_begin() { return RewriteBuffers.begin(); }<br>
buffer_iterator buffer_end() { return RewriteBuffers.end(); }<br>
<br>
+ /// SaveFiles - Save all changed files to disk.<br>
+ ///<br>
+ /// Returns whether not all changes were saved successfully.<br>
+ /// Outputs diagnostics via the source manager's diagnostic engine<br>
+ /// in case of an error.<br>
+ bool overwriteChangedFiles();<br>
+<br>
private:<br>
unsigned getLocationOffsetAndFileID(SourceLocation Loc, FileID &FID) const;<br>
};<br>
<br>
Modified: cfe/trunk/lib/Rewrite/Rewriter.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/Rewriter.cpp?rev=157260&r1=157259&r2=157260&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/Rewriter.cpp?rev=157260&r1=157259&r2=157260&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Rewrite/Rewriter.cpp (original)<br>
+++ cfe/trunk/lib/Rewrite/Rewriter.cpp Tue May 22 12:01:35 2012<br>
@@ -15,9 +15,12 @@<br>
#include "clang/Rewrite/Rewriter.h"<br>
#include "clang/AST/Stmt.h"<br>
#include "clang/AST/Decl.h"<br>
-#include "clang/Lex/Lexer.h"<br>
+#include "clang/Basic/DiagnosticIDs.h"<br>
+#include "clang/Basic/FileManager.h"<br>
#include "clang/Basic/SourceManager.h"<br>
+#include "clang/Lex/Lexer.h"<br>
#include "llvm/ADT/SmallString.h"<br>
+#include "llvm/Support/FileSystem.h"<br>
using namespace clang;<br>
<br>
raw_ostream &RewriteBuffer::write(raw_ostream &os) const {<br>
@@ -412,3 +415,68 @@<br>
<br>
return false;<br>
}<br>
+<br>
+// A wrapper for a file stream that atomically overwrites the target.<br>
+//<br>
+// Creates a file output stream for a temporary file in the constructor,<br>
+// which is later accessible via getStream() if ok() return true.<br>
+// Flushes the stream and moves the temporary file to the target location<br>
+// in the destructor.<br>
+class AtomicallyMovedFile {<br>
+public:<br>
+ AtomicallyMovedFile(DiagnosticsEngine &Diagnostics, StringRef Filename,<br>
+ bool &AllWritten)<br>
+ : Diagnostics(Diagnostics), Filename(Filename), AllWritten(AllWritten) {<br>
+ TempFilename = Filename;<br>
+ TempFilename += "-%%%%%%%%";<br>
+ int FD;<br>
+ if (llvm::sys::fs::unique_file(TempFilename.str(), FD, TempFilename,<br>
+ /*makeAbsolute=*/true, 0664)) {<br>
+ AllWritten = false;<br>
+ Diagnostics.Report(clang::diag::err_unable_to_make_temp)<br>
+ << TempFilename;<br>
+ } else {<br>
+ FileStream.reset(new llvm::raw_fd_ostream(FD, /*shouldClose=*/true));<br>
+ }<br>
+ }<br>
+<br>
+ ~AtomicallyMovedFile() {<br>
+ if (!ok()) return;<br>
+<br>
+ FileStream->flush();<br>
+ if (llvm::error_code ec =<br>
+ llvm::sys::fs::rename(TempFilename.str(), Filename)) {<br>
+ AllWritten = false;<br>
+ Diagnostics.Report(clang::diag::err_unable_to_rename_temp)<br>
+ << TempFilename << Filename << ec.message();<br>
+ bool existed;<br>
+ // If the remove fails, there's not a lot we can do - this is already an<br>
+ // error.<br>
+ llvm::sys::fs::remove(TempFilename.str(), existed);<br>
+ }<br>
+ }<br>
+<br>
+ bool ok() { return FileStream; }<br>
+ llvm::raw_ostream &getStream() { return *FileStream; }<br>
+<br>
+private:<br>
+ DiagnosticsEngine &Diagnostics;<br>
+ StringRef Filename;<br>
+ SmallString<128> TempFilename;<br>
+ OwningPtr<llvm::raw_fd_ostream> FileStream;<br>
+ bool &AllWritten;<br>
+};<br>
+<br>
+bool Rewriter::overwriteChangedFiles() {<br>
+ bool AllWritten = true;<br>
+ for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {<br>
+ const FileEntry *Entry =<br>
+ getSourceMgr().getFileEntryForID(I->first);<br>
+ AtomicallyMovedFile File(getSourceMgr().getDiagnostics(), Entry->getName(),<br>
+ AllWritten);<br>
+ if (File.ok()) {<br>
+ I->second.write(File.getStream());<br>
+ }<br>
+ }<br>
+ return !AllWritten;<br>
+}<br>
<br>
Modified: cfe/trunk/unittests/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/CMakeLists.txt?rev=157260&r1=157259&r2=157260&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/CMakeLists.txt?rev=157260&r1=157259&r2=157260&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/unittests/CMakeLists.txt (original)<br>
+++ cfe/trunk/unittests/CMakeLists.txt Tue May 22 12:01:35 2012<br>
@@ -70,5 +70,6 @@<br>
Tooling/CompilationDatabaseTest.cpp<br>
Tooling/ToolingTest.cpp<br>
Tooling/RecursiveASTVisitorTest.cpp<br>
- USED_LIBS gtest gtest_main clangAST clangTooling<br>
+ Tooling/RewriterTest.cpp<br>
+ USED_LIBS gtest gtest_main clangAST clangTooling clangRewrite<br>
)<br>
<br>
Added: cfe/trunk/unittests/Tooling/RewriterTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RewriterTest.cpp?rev=157260&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RewriterTest.cpp?rev=157260&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/unittests/Tooling/RewriterTest.cpp (added)<br>
+++ cfe/trunk/unittests/Tooling/RewriterTest.cpp Tue May 22 12:01:35 2012<br>
@@ -0,0 +1,37 @@<br>
+//===- unittest/Tooling/RewriterTest.cpp ----------------------------------===//<br>
+//<br>
+// The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "RewriterTestContext.h"<br>
+#include "gtest/gtest.h"<br>
+<br>
+namespace clang {<br>
+<br>
+TEST(Rewriter, OverwritesChangedFiles) {<br>
+ RewriterTestContext Context;<br>
+ FileID ID = Context.createOnDiskFile("t.cpp", "line1\nline2\nline3\nline4");<br>
+ Context.Rewrite.ReplaceText(Context.getLocation(ID, 2, 1), 5, "replaced");<br>
+ EXPECT_FALSE(Context.Rewrite.overwriteChangedFiles());<br>
+ EXPECT_EQ("line1\nreplaced\nline3\nline4",<br>
+ Context.getFileContentFromDisk("t.cpp"));<br>
+}<br>
+<br>
+TEST(Rewriter, ContinuesOverwritingFilesOnError) {<br>
+ RewriterTestContext Context;<br>
+ FileID FailingID = Context.createInMemoryFile("invalid/failing.cpp", "test");<br>
+ Context.Rewrite.ReplaceText(Context.getLocation(FailingID, 1, 2), 1, "other");<br>
+ FileID WorkingID = Context.createOnDiskFile(<br>
+ "working.cpp", "line1\nline2\nline3\nline4");<br>
+ Context.Rewrite.ReplaceText(Context.getLocation(WorkingID, 2, 1), 5,<br>
+ "replaced");<br>
+ EXPECT_TRUE(Context.Rewrite.overwriteChangedFiles());<br>
+ EXPECT_EQ("line1\nreplaced\nline3\nline4",<br>
+ Context.getFileContentFromDisk("working.cpp"));<br>
+}<br>
+<br>
+} // end namespace clang<br>
<br>
Propchange: cfe/trunk/unittests/Tooling/RewriterTest.cpp<br>
------------------------------------------------------------------------------<br>
svn:eol-style = LF<br>
<br>
Added: cfe/trunk/unittests/Tooling/RewriterTestContext.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RewriterTestContext.h?rev=157260&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RewriterTestContext.h?rev=157260&view=auto</a><br>
==============================================================================<br>
--- cfe/trunk/unittests/Tooling/RewriterTestContext.h (added)<br>
+++ cfe/trunk/unittests/Tooling/RewriterTestContext.h Tue May 22 12:01:35 2012<br>
@@ -0,0 +1,120 @@<br>
+//===--- RewriterTestContext.h ----------------------------------*- C++ -*-===//<br>
+//<br>
+// The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file defines a utility class for Rewriter related tests.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_CLANG_REWRITER_TEST_CONTEXT_H<br>
+#define LLVM_CLANG_REWRITER_TEST_CONTEXT_H<br>
+<br>
+#include "clang/Basic/Diagnostic.h"<br>
+#include "clang/Basic/FileManager.h"<br>
+#include "clang/Basic/LangOptions.h"<br>
+#include "clang/Basic/SourceManager.h"<br>
+#include "clang/Frontend/DiagnosticOptions.h"<br>
+#include "clang/Frontend/TextDiagnosticPrinter.h"<br>
+#include "clang/Rewrite/Rewriter.h"<br>
+#include "llvm/Support/Path.h"<br>
+#include "llvm/Support/raw_ostream.h"<br>
+<br>
+namespace clang {<br>
+<br>
+/// \brief A class that sets up a ready to use Rewriter.<br>
+///<br>
+/// Useful in unit tests that need a Rewriter. Creates all dependencies<br>
+/// of a Rewriter with default values for testing and provides convenience<br>
+/// methods, which help with writing tests that change files.<br>
+class RewriterTestContext {<br>
+ public:<br>
+ RewriterTestContext()<br>
+ : Diagnostics(llvm::IntrusiveRefCntPtr<DiagnosticIDs>()),<br>
+ DiagnosticPrinter(llvm::outs(), DiagnosticOptions()),<br>
+ Files((FileSystemOptions())),<br>
+ Sources(Diagnostics, Files),<br>
+ Rewrite(Sources, Options) {<br>
+ Diagnostics.setClient(&DiagnosticPrinter, false);<br>
+ }<br>
+<br>
+ ~RewriterTestContext() {<br>
+ if (TemporaryDirectory.isValid()) {<br>
+ std::string ErrorInfo;<br>
+ TemporaryDirectory.eraseFromDisk(true, &ErrorInfo);<br>
+ assert(ErrorInfo.empty());<br>
+ }<br>
+ }<br>
+<br>
+ FileID createInMemoryFile(StringRef Name, StringRef Content) {<br>
+ const llvm::MemoryBuffer *Source =<br>
+ llvm::MemoryBuffer::getMemBuffer(Content);<br>
+ const FileEntry *Entry =<br>
+ Files.getVirtualFile(Name, Source->getBufferSize(), 0);<br>
+ Sources.overrideFileContents(Entry, Source, true);<br>
+ assert(Entry != NULL);<br>
+ return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User);<br>
+ }<br>
+<br>
+ FileID createOnDiskFile(StringRef Name, StringRef Content) {<br>
+ if (!TemporaryDirectory.isValid()) {<br>
+ std::string ErrorInfo;<br>
+ TemporaryDirectory = llvm::sys::Path::GetTemporaryDirectory(&ErrorInfo);<br>
+ assert(ErrorInfo.empty());<br>
+ }<br>
+ llvm::SmallString<1024> Path(TemporaryDirectory.str());<br>
+ llvm::sys::path::append(Path, Name);<br>
+ std::string ErrorInfo;<br>
+ llvm::raw_fd_ostream OutStream(Path.c_str(),<br>
+ ErrorInfo, llvm::raw_fd_ostream::F_Binary);<br>
+ assert(ErrorInfo.empty());<br>
+ OutStream << Content;<br>
+ OutStream.close();<br>
+ const FileEntry *File = Files.getFile(Path);<br>
+ assert(File != NULL);<br>
+ return Sources.createFileID(File, SourceLocation(), SrcMgr::C_User);<br>
+ }<br>
+<br>
+ SourceLocation getLocation(FileID ID, unsigned Line, unsigned Column) {<br>
+ SourceLocation Result = Sources.translateFileLineCol(<br>
+ Sources.getFileEntryForID(ID), Line, Column);<br>
+ assert(Result.isValid());<br>
+ return Result;<br>
+ }<br>
+<br>
+ std::string getRewrittenText(FileID ID) {<br>
+ std::string Result;<br>
+ llvm::raw_string_ostream OS(Result);<br>
+ Rewrite.getEditBuffer(ID).write(OS);<br>
+ return Result;<br>
+ }<br>
+<br>
+ std::string getFileContentFromDisk(StringRef Name) {<br>
+ llvm::SmallString<1024> Path(TemporaryDirectory.str());<br>
+ llvm::sys::path::append(Path, Name);<br>
+ // We need to read directly from the FileManager without relaying through<br>
+ // a FileEntry, as otherwise we'd read through an already opened file<br>
+ // descriptor, which might not see the changes made.<br>
+ // FIXME: Figure out whether there is a way to get the SourceManger to<br>
+ // reopen the file.<br>
+ return Files.getBufferForFile(Path, NULL)->getBuffer();<br>
+ }<br>
+<br>
+ DiagnosticsEngine Diagnostics;<br>
+ TextDiagnosticPrinter DiagnosticPrinter;<br>
+ FileManager Files;<br>
+ SourceManager Sources;<br>
+ LangOptions Options;<br>
+ Rewriter Rewrite;<br>
+<br>
+ // Will be set once on disk files are generated.<br>
+ llvm::sys::Path TemporaryDirectory;<br>
+};<br>
+<br>
+} // end namespace clang<br>
+<br>
+#endif<br>
<br>
Propchange: cfe/trunk/unittests/Tooling/RewriterTestContext.h<br>
------------------------------------------------------------------------------<br>
svn:eol-style = LF<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu" target="_blank">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div></div></div>
</blockquote></div><br>