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

llvm-svn: 172399
This commit is contained in:
Alexander Kornienko 2013-01-14 11:34:14 +00:00
parent 66b35642d4
commit 116ba68220
3 changed files with 21 additions and 13 deletions

View File

@ -25,6 +25,7 @@ namespace clang {
class Lexer; class Lexer;
class SourceManager; class SourceManager;
class DiagnosticConsumer;
namespace format { namespace format {
@ -84,11 +85,15 @@ FormatStyle getGoogleStyle();
/// everything that might influence its formatting or might be influenced by its /// everything that might influence its formatting or might be influenced by its
/// formatting. /// 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 /// Returns the \c Replacements necessary to make all \p Ranges comply with
/// \p Style. /// \p Style.
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
SourceManager &SourceMgr, 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. /// \brief Returns the \c LangOpts that the formatter expects you to set.
LangOptions getFormattingLangOpts(); LangOptions getFormattingLangOpts();

View File

@ -1331,7 +1331,7 @@ private:
class Formatter : public UnwrappedLineConsumer { class Formatter : public UnwrappedLineConsumer {
public: public:
Formatter(clang::DiagnosticsEngine &Diag, const FormatStyle &Style, Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style,
Lexer &Lex, SourceManager &SourceMgr, Lexer &Lex, SourceManager &SourceMgr,
const std::vector<CharSourceRange> &Ranges) const std::vector<CharSourceRange> &Ranges)
: Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr), : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
@ -1517,7 +1517,7 @@ private:
return Indent; return Indent;
} }
clang::DiagnosticsEngine &Diag; DiagnosticsEngine &Diag;
FormatStyle Style; FormatStyle Style;
Lexer &Lex; Lexer &Lex;
SourceManager &SourceMgr; SourceManager &SourceMgr;
@ -1529,13 +1529,18 @@ private:
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
SourceManager &SourceMgr, SourceManager &SourceMgr,
std::vector<CharSourceRange> Ranges) { std::vector<CharSourceRange> Ranges,
DiagnosticConsumer *DiagClient) {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts); OwningPtr<DiagnosticConsumer> DiagPrinter;
DiagnosticPrinter.BeginSourceFile(Lex.getLangOpts(), Lex.getPP()); if (DiagClient == 0) {
DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
DiagClient = DiagPrinter.get();
}
DiagnosticsEngine Diagnostics( DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
&DiagnosticPrinter, false); DiagClient, false);
Diagnostics.setSourceManager(&SourceMgr); Diagnostics.setSourceManager(&SourceMgr);
Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges); Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
return formatter.format(); return formatter.format();

View File

@ -29,7 +29,8 @@ protected:
Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
getFormattingLangOpts()); getFormattingLangOpts());
tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, tooling::Replacements Replace = reformat(Style, Lex, Context.Sources,
Ranges); Ranges,
new IgnoringDiagConsumer());
EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite));
return Context.getRewrittenText(ID); return Context.getRewrittenText(ID);
} }
@ -1206,8 +1207,6 @@ TEST_F(FormatTest, IncorrectCodeErrorDetection) {
EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n"));
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = 10;
EXPECT_EQ("{\n" EXPECT_EQ("{\n"
" {\n" " {\n"
" breakme(\n" " breakme(\n"
@ -1215,8 +1214,7 @@ TEST_F(FormatTest, IncorrectCodeErrorDetection) {
"}\n", format("{\n" "}\n", format("{\n"
" {\n" " {\n"
" breakme(qwe);\n" " breakme(qwe);\n"
"}\n", Style)); "}\n", getLLVMStyleWithColumns(10)));
} }
TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {