#include "clang/Format/Format.h"
#include "UnwrappedLineParser.h"
+#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/OperatorPrecedence.h"
#include "clang/Basic/SourceManager.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Lex/Lexer.h"
#include <string>
class Formatter : public UnwrappedLineConsumer {
public:
- Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
+ Formatter(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
+ Lexer &Lex, SourceManager &SourceMgr,
const std::vector<CharSourceRange> &Ranges)
- : Style(Style), Lex(Lex), SourceMgr(SourceMgr), Ranges(Ranges),
- StructuralError(false) {
+ : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
+ Ranges(Ranges), StructuralError(false) {
}
virtual ~Formatter() {
tooling::Replacements format() {
LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
- UnwrappedLineParser Parser(Style, Tokens, *this);
+ UnwrappedLineParser Parser(Diag, Style, Tokens, *this);
StructuralError = Parser.parse();
unsigned PreviousEndOfLineColumn = 0;
for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),
1;
}
+ clang::DiagnosticsEngine &Diag;
FormatStyle Style;
Lexer &Lex;
SourceManager &SourceMgr;
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
SourceManager &SourceMgr,
std::vector<CharSourceRange> Ranges) {
- Formatter formatter(Style, Lex, SourceMgr, Ranges);
+ IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
+ TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
+ DiagnosticPrinter.BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
+ DiagnosticsEngine Diagnostics(
+ llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
+ &DiagnosticPrinter, false);
+ Diagnostics.setSourceManager(&SourceMgr);
+ Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
return formatter.format();
}
//===----------------------------------------------------------------------===//
#include "UnwrappedLineParser.h"
+#include "clang/Basic/Diagnostic.h"
#include "llvm/Support/raw_ostream.h"
// Uncomment to get debug output from the UnwrappedLineParser.
bool PreBlockRootTokenInitialized;
};
-UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
- FormatTokenSource &Tokens,
- UnwrappedLineConsumer &Callback)
+UnwrappedLineParser::UnwrappedLineParser(
+ clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
+ FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback)
: Line(new UnwrappedLine), RootTokenInitialized(false),
- LastInCurrentLine(NULL), MustBreakBeforeNextToken(false), Style(Style),
- Tokens(&Tokens), Callback(Callback) {
+ LastInCurrentLine(NULL), MustBreakBeforeNextToken(false), Diag(Diag),
+ Style(Style), Tokens(&Tokens), Callback(Callback) {
}
bool UnwrappedLineParser::parse() {
if (HasOpeningBrace) {
return false;
} else {
- // Stray '}' is an error.
+ Diag.Report(FormatTok.Tok.getLocation(),
+ Diag.getCustomDiagID(clang::DiagnosticsEngine::Error,
+ "Stray '}' found"));
Error = true;
nextToken();
addUnwrappedLine();
#include <vector>
namespace clang {
+
+class DiagnosticsEngine;
+
namespace format {
/// \brief A wrapper around a \c Token storing information about the
class UnwrappedLineParser {
public:
- UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens,
+ UnwrappedLineParser(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
+ FormatTokenSource &Tokens,
UnwrappedLineConsumer &Callback);
/// Returns true in case of a structural error.
FormatToken FormatTok;
bool MustBreakBeforeNextToken;
+ clang::DiagnosticsEngine &Diag;
const FormatStyle &Style;
FormatTokenSource *Tokens;
UnwrappedLineConsumer &Callback;