[cfe-commits] [PATCH] X-TU Refactoring support

Douglas Gregor dgregor at apple.com
Fri Apr 27 10:49:07 PDT 2012


On Apr 19, 2012, at 4:30 AM, Manuel Klimek <klimek at google.com> wrote:

> The attached patch adds a layer on top of the tooling support for
> cross-TU refactorings.


+static const char * const InvalidLocation = "invalid-location";
+
+Replacement::Replacement()
+  : FilePath(InvalidLocation), Offset(0), Length(0) {}
+

If we have to pick a sentinel, can it at least be an empty FilePath?

+bool saveRewrittenFiles(Rewriter &Rewrite) {
+  for (Rewriter::buffer_iterator I = Rewrite.buffer_begin(),
+                                 E = Rewrite.buffer_end();
+       I != E; ++I) {
+    // FIXME: This code is copied from the FixItRewriter.cpp - I think it should
+    // go into directly into Rewriter (there we also have the Diagnostics to
+    // handle the error cases better).
+    const FileEntry *Entry =
+        Rewrite.getSourceMgr().getFileEntryForID(I->first);
+    std::string ErrorInfo;
+    llvm::raw_fd_ostream FileStream(
+        Entry->getName(), ErrorInfo, llvm::raw_fd_ostream::F_Binary);
+    if (!ErrorInfo.empty())
+      return false;
+    I->second.write(FileStream);
+    FileStream.flush();
+  }
+  return true;
+}

I agree that this should go into the Rewriter. Go for it :)

+int getRangeSize(SourceManager &Sources, const CharSourceRange &Range) {
+  SourceLocation SpellingBegin = Sources.getSpellingLoc(Range.getBegin());
+  SourceLocation SpellingEnd = Sources.getSpellingLoc(Range.getEnd());
+  std::pair<FileID, unsigned> Start = Sources.getDecomposedLoc(SpellingBegin);
+  std::pair<FileID, unsigned> End = Sources.getDecomposedLoc(SpellingEnd);
+  if (Start.first != End.first) return -1;
+  if (Range.isTokenRange())
+    // FIXME: Bring in the correct LangOptions.
+    End.second += Lexer::MeasureTokenLength(SpellingEnd, Sources,
+                                            LangOptions());
+  return End.second - Start.second;
+}

This should probably return an 'unsigned'. It belongs in the Lexer, IMO.

I'm not sure whether you've looked at the Edit library or not, but its functionality fits well with what you're doing here, because it can order rewrites such that a given set of rewrite operations is more likely to succeed. For example, if one rewrite edits part of a function body and another rewrite removes that function entirely, the edit library will deal with it whereas the rewriter by itself could fail. There's no specific action item here, other than a suggestion that there's some potential goodness in combining these features.

Anyway, with those fairly minor changes, this can go in. Thanks!

	- Doug



More information about the cfe-commits mailing list