[cfe-commits] r172399 - in /cfe/trunk: include/clang/Format/Format.h lib/Format/Format.cpp unittests/Format/FormatTest.cpp
Alexander Kornienko
alexfh at google.com
Mon Jan 14 03:34:14 PST 2013
Author: alexfh
Date: Mon Jan 14 05:34:14 2013
New Revision: 172399
URL: http://llvm.org/viewvc/llvm-project?rev=172399&view=rev
Log:
Custom DiagnosticConsumer parameter of reformat() + silence diagnostics in unit tests.
Summary:
Added tests for clang-format diagnostics. Added DiagnosticConsumer
argument to clang::format::reformat().
Reviewers: klimek, djasper
Reviewed By: djasper
CC: cfe-commits, thakis, rafael.espindola
Differential Revision: http://llvm-reviews.chandlerc.com/D290
Modified:
cfe/trunk/include/clang/Format/Format.h
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=172399&r1=172398&r2=172399&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Mon Jan 14 05:34:14 2013
@@ -25,6 +25,7 @@
class Lexer;
class SourceManager;
+class DiagnosticConsumer;
namespace format {
@@ -57,7 +58,7 @@
unsigned SpacesBeforeTrailingComments;
/// \brief If the constructor initializers don't fit on a line, put each
- /// initializer on its own line.
+ /// initializer on its own line.
bool ConstructorInitializerAllOnOneLineOrOnePerLine;
/// \brief Add a space in front of an Objective-C protocol list, i.e. use
@@ -84,11 +85,15 @@
/// everything that might influence its formatting or might be influenced by its
/// formatting.
///
+/// \param DiagClient A custom DiagnosticConsumer. Can be 0, in this case
+/// diagnostic is output to llvm::errs().
+///
/// Returns the \c Replacements necessary to make all \p Ranges comply with
/// \p Style.
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
SourceManager &SourceMgr,
- std::vector<CharSourceRange> Ranges);
+ std::vector<CharSourceRange> Ranges,
+ DiagnosticConsumer *DiagClient = 0);
/// \brief Returns the \c LangOpts that the formatter expects you to set.
LangOptions getFormattingLangOpts();
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172399&r1=172398&r2=172399&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jan 14 05:34:14 2013
@@ -1331,7 +1331,7 @@
class Formatter : public UnwrappedLineConsumer {
public:
- Formatter(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
+ Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style,
Lexer &Lex, SourceManager &SourceMgr,
const std::vector<CharSourceRange> &Ranges)
: Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
@@ -1517,7 +1517,7 @@
return Indent;
}
- clang::DiagnosticsEngine &Diag;
+ DiagnosticsEngine &Diag;
FormatStyle Style;
Lexer &Lex;
SourceManager &SourceMgr;
@@ -1529,13 +1529,18 @@
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
SourceManager &SourceMgr,
- std::vector<CharSourceRange> Ranges) {
+ std::vector<CharSourceRange> Ranges,
+ DiagnosticConsumer *DiagClient) {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
- TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
- DiagnosticPrinter.BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
+ OwningPtr<DiagnosticConsumer> DiagPrinter;
+ if (DiagClient == 0) {
+ DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
+ DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
+ DiagClient = DiagPrinter.get();
+ }
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
- &DiagnosticPrinter, false);
+ DiagClient, false);
Diagnostics.setSourceManager(&SourceMgr);
Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
return formatter.format();
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172399&r1=172398&r2=172399&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 14 05:34:14 2013
@@ -29,7 +29,8 @@
Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
getFormattingLangOpts());
tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
- Ranges);
+ Ranges,
+ new IgnoringDiagConsumer());
EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
return Context.getRewrittenText(ID);
}
@@ -1206,8 +1207,6 @@
EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
- FormatStyle Style = getLLVMStyle();
- Style.ColumnLimit = 10;
EXPECT_EQ("{\n"
" {\n"
" breakme(\n"
@@ -1215,8 +1214,7 @@
"}\n", format("{\n"
" {\n"
" breakme(qwe);\n"
- "}\n", Style));
-
+ "}\n", getLLVMStyleWithColumns(10)));
}
TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
More information about the cfe-commits
mailing list