[PATCH] Add a more convenient interface to use the clang-format library.
Daniel Jasper
djasper at google.com
Tue May 14 09:47:44 PDT 2013
Hi klimek,
It turns out that several implementations go through the trouble of setting up a SourceManager and Lexer and abstracting this into a function makes usage easier.
Also abstracts SourceManager-independent ranges out of tooling::Refactoring and provides a convenience function to create them from line ranges.
So far, this only contains the interface, nothing is implemented yet.
http://llvm-reviews.chandlerc.com/D793
Files:
include/clang/Format/Format.h
include/clang/Tooling/Refactoring.h
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -187,6 +187,13 @@
std::vector<CharSourceRange> Ranges,
DiagnosticConsumer *DiagClient = 0);
+/// \brief Reformats the given \p Ranges in \p Code.
+///
+/// Otherwise identical to the reformat() function consuming a \c Lexer.
+tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
+ std::vector<tooling::Range> Ranges,
+ DiagnosticConsumer *DiagClient = 0);
+
/// \brief Returns the \c LangOpts that the formatter expects you to set.
LangOptions getFormattingLangOpts();
Index: include/clang/Tooling/Refactoring.h
===================================================================
--- include/clang/Tooling/Refactoring.h
+++ include/clang/Tooling/Refactoring.h
@@ -32,6 +32,25 @@
namespace tooling {
+/// \brief A source range independent of the \c SourceManager.
+class Range {
+public:
+ Range(unsigned Offset, unsigned Length);
+
+ unsigned getOffset() const { return Offset; }
+ unsigned getLength() const { return Length; }
+
+private:
+ unsigned Offset;
+ unsigned Length;
+};
+
+/// \brief Returns a \c Range corresponding to the given line range in \p Code.
+///
+/// Returns the closest possible range if \p StartLine or \p EndLine are partly
+/// outside of \p Code.
+Range getLineRange(StringRef Code, unsigned StartLine, unsigned EndLine);
+
/// \brief A text replacement.
///
/// Represents a SourceManager independent replacement of a range of text in a
@@ -72,8 +91,8 @@
/// \brief Accessors.
/// @{
StringRef getFilePath() const { return FilePath; }
- unsigned getOffset() const { return Offset; }
- unsigned getLength() const { return Length; }
+ unsigned getOffset() const { return ReplacementRange.getOffset(); }
+ unsigned getLength() const { return ReplacementRange.getLength(); }
StringRef getReplacementText() const { return ReplacementText; }
/// @}
@@ -96,8 +115,7 @@
StringRef ReplacementText);
std::string FilePath;
- unsigned Offset;
- unsigned Length;
+ Range ReplacementRange;
std::string ReplacementText;
};
@@ -113,6 +131,10 @@
/// \returns true if all replacements apply. false otherwise.
bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite);
+/// \brief Applies all replacements in \p Replaces to \p Code.
+///
+/// This completely ignores the path stored in each replacement. If one or more
+/// replacements cannot be applied, this returns an empty \c string.
std::string applyAllReplacements(StringRef Code, Replacements &Replaces);
/// \brief A tool to run refactorings.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D793.1.patch
Type: text/x-patch
Size: 2833 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130514/7cc4e79e/attachment.bin>
More information about the cfe-commits
mailing list