2012-12-04 17:13:33 +08:00
|
|
|
//===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===//
|
2012-12-04 02:12:45 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file contains the declaration of the UnwrappedLineParser,
|
|
|
|
/// which turns a stream of tokens into UnwrappedLines.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
|
|
|
|
#define LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
|
|
|
|
|
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-12-07 02:03:27 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2012-12-04 02:12:45 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2013-01-16 17:10:19 +08:00
|
|
|
#include <list>
|
2013-01-08 22:56:18 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
namespace clang {
|
2013-01-10 23:05:09 +08:00
|
|
|
|
|
|
|
class DiagnosticsEngine;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
namespace format {
|
|
|
|
|
|
|
|
/// \brief A wrapper around a \c Token storing information about the
|
|
|
|
/// whitespace characters preceeding it.
|
|
|
|
struct FormatToken {
|
2013-01-03 00:30:12 +08:00
|
|
|
FormatToken()
|
2013-01-06 06:56:06 +08:00
|
|
|
: NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
|
2013-03-04 21:43:19 +08:00
|
|
|
LastNewlineOffset(0), TokenLength(0), IsFirst(false),
|
|
|
|
MustBreakBefore(false) {}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
/// \brief The \c Token.
|
|
|
|
Token Tok;
|
|
|
|
|
|
|
|
/// \brief The number of newlines immediately before the \c Token.
|
|
|
|
///
|
|
|
|
/// This can be used to determine what the user wrote in the original code
|
|
|
|
/// and thereby e.g. leave an empty line between two function definitions.
|
|
|
|
unsigned NewlinesBefore;
|
|
|
|
|
2013-01-03 00:30:12 +08:00
|
|
|
/// \brief Whether there is at least one unescaped newline before the \c
|
|
|
|
/// Token.
|
|
|
|
bool HasUnescapedNewline;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
/// \brief The location of the start of the whitespace immediately preceeding
|
|
|
|
/// the \c Token.
|
|
|
|
///
|
|
|
|
/// Used together with \c WhiteSpaceLength to create a \c Replacement.
|
|
|
|
SourceLocation WhiteSpaceStart;
|
|
|
|
|
|
|
|
/// \brief The length in characters of the whitespace immediately preceeding
|
|
|
|
/// the \c Token.
|
|
|
|
unsigned WhiteSpaceLength;
|
2013-01-06 06:56:06 +08:00
|
|
|
|
2013-03-04 21:43:19 +08:00
|
|
|
/// \brief The offset just past the last '\n' in this token's leading
|
|
|
|
/// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
|
|
|
|
unsigned LastNewlineOffset;
|
|
|
|
|
2013-01-07 15:56:50 +08:00
|
|
|
/// \brief The length of the non-whitespace parts of the token. This is
|
|
|
|
/// necessary because we need to handle escaped newlines that are stored
|
|
|
|
/// with the token.
|
|
|
|
unsigned TokenLength;
|
|
|
|
|
2013-01-06 06:56:06 +08:00
|
|
|
/// \brief Indicates that this is the first token.
|
|
|
|
bool IsFirst;
|
2013-01-08 22:56:18 +08:00
|
|
|
|
2013-01-09 23:25:02 +08:00
|
|
|
/// \brief Whether there must be a line break before this token.
|
|
|
|
///
|
|
|
|
/// This happens for example when a preprocessor directive ended directly
|
|
|
|
/// before the token.
|
|
|
|
bool MustBreakBefore;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief An unwrapped line is a sequence of \c Token, that we would like to
|
|
|
|
/// put on a single line if there was no column limit.
|
|
|
|
///
|
|
|
|
/// This is used as a main interface between the \c UnwrappedLineParser and the
|
|
|
|
/// \c UnwrappedLineFormatter. The key property is that changing the formatting
|
|
|
|
/// within an unwrapped line does not affect any other unwrapped lines.
|
|
|
|
struct UnwrappedLine {
|
2013-01-23 17:32:48 +08:00
|
|
|
UnwrappedLine() : Level(0), InPPDirective(false), MustBeDeclaration(false) {
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-16 18:41:46 +08:00
|
|
|
// FIXME: Don't use std::list here.
|
2013-01-16 17:10:19 +08:00
|
|
|
/// \brief The \c Tokens comprising this \c UnwrappedLine.
|
|
|
|
std::list<FormatToken> Tokens;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
/// \brief The indent level of the \c UnwrappedLine.
|
|
|
|
unsigned Level;
|
2013-01-03 00:30:12 +08:00
|
|
|
|
|
|
|
/// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
|
|
|
|
bool InPPDirective;
|
2013-01-23 17:32:48 +08:00
|
|
|
|
|
|
|
bool MustBeDeclaration;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class UnwrappedLineConsumer {
|
|
|
|
public:
|
2012-12-05 05:05:31 +08:00
|
|
|
virtual ~UnwrappedLineConsumer() {
|
|
|
|
}
|
2012-12-05 21:56:52 +08:00
|
|
|
virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2012-12-08 00:15:44 +08:00
|
|
|
class FormatTokenSource {
|
|
|
|
public:
|
2012-12-08 06:49:27 +08:00
|
|
|
virtual ~FormatTokenSource() {
|
|
|
|
}
|
2012-12-08 00:15:44 +08:00
|
|
|
virtual FormatToken getNextToken() = 0;
|
|
|
|
};
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
class UnwrappedLineParser {
|
|
|
|
public:
|
2013-01-10 23:05:09 +08:00
|
|
|
UnwrappedLineParser(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
|
|
|
|
FormatTokenSource &Tokens,
|
2012-12-04 02:12:45 +08:00
|
|
|
UnwrappedLineConsumer &Callback);
|
|
|
|
|
2012-12-05 01:27:50 +08:00
|
|
|
/// Returns true in case of a structural error.
|
|
|
|
bool parse();
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
private:
|
2013-01-05 07:34:14 +08:00
|
|
|
bool parseFile();
|
2013-01-07 04:07:31 +08:00
|
|
|
bool parseLevel(bool HasOpeningBrace);
|
2013-01-23 17:32:48 +08:00
|
|
|
bool parseBlock(bool MustBeDeclaration, unsigned AddLevels = 1);
|
2012-12-04 02:12:45 +08:00
|
|
|
void parsePPDirective();
|
2013-01-05 07:34:14 +08:00
|
|
|
void parsePPDefine();
|
|
|
|
void parsePPUnknown();
|
2013-01-07 22:56:16 +08:00
|
|
|
void parseStructuralElement();
|
2013-01-10 19:52:21 +08:00
|
|
|
void parseBracedList();
|
2013-01-21 18:07:49 +08:00
|
|
|
void parseReturn();
|
2012-12-04 02:12:45 +08:00
|
|
|
void parseParens();
|
|
|
|
void parseIfThenElse();
|
2012-12-05 23:06:06 +08:00
|
|
|
void parseForOrWhileLoop();
|
2012-12-04 02:12:45 +08:00
|
|
|
void parseDoWhile();
|
|
|
|
void parseLabel();
|
|
|
|
void parseCaseLabel();
|
|
|
|
void parseSwitch();
|
2012-12-07 02:03:27 +08:00
|
|
|
void parseNamespace();
|
2012-12-04 02:12:45 +08:00
|
|
|
void parseAccessSpecifier();
|
|
|
|
void parseEnum();
|
2013-01-15 21:38:33 +08:00
|
|
|
void parseRecord();
|
2013-01-10 05:15:03 +08:00
|
|
|
void parseObjCProtocolList();
|
|
|
|
void parseObjCUntilAtEnd();
|
2013-01-10 07:25:37 +08:00
|
|
|
void parseObjCInterfaceOrImplementation();
|
2013-01-10 05:15:03 +08:00
|
|
|
void parseObjCProtocol();
|
2012-12-04 02:12:45 +08:00
|
|
|
void addUnwrappedLine();
|
|
|
|
bool eof() const;
|
|
|
|
void nextToken();
|
2013-01-05 07:34:14 +08:00
|
|
|
void readToken();
|
2013-01-23 00:31:55 +08:00
|
|
|
void flushComments(bool NewlineBeforeNext);
|
|
|
|
void pushToken(const FormatToken &Tok);
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-06 06:14:16 +08:00
|
|
|
// FIXME: We are constantly running into bugs where Line.Level is incorrectly
|
|
|
|
// subtracted from beyond 0. Introduce a method to subtract from Line.Level
|
|
|
|
// and use that everywhere in the Parser.
|
2013-01-13 03:30:44 +08:00
|
|
|
OwningPtr<UnwrappedLine> Line;
|
2013-01-23 00:31:55 +08:00
|
|
|
|
|
|
|
// Comments are sorted into unwrapped lines by whether they are in the same
|
|
|
|
// line as the previous token, or not. If not, they belong to the next token.
|
|
|
|
// Since the next token might already be in a new unwrapped line, we need to
|
|
|
|
// store the comments belonging to that token.
|
|
|
|
SmallVector<FormatToken, 1> CommentsBeforeNextToken;
|
2012-12-04 02:12:45 +08:00
|
|
|
FormatToken FormatTok;
|
2013-01-09 23:25:02 +08:00
|
|
|
bool MustBreakBeforeNextToken;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-19 02:24:28 +08:00
|
|
|
// The parsed lines. Only added to through \c CurrentLines.
|
2013-01-18 22:04:34 +08:00
|
|
|
std::vector<UnwrappedLine> Lines;
|
|
|
|
|
|
|
|
// Preprocessor directives are parsed out-of-order from other unwrapped lines.
|
|
|
|
// Thus, we need to keep a list of preprocessor directives to be reported
|
|
|
|
// after an unwarpped line that has been started was finished.
|
|
|
|
std::vector<UnwrappedLine> PreprocessorDirectives;
|
|
|
|
|
|
|
|
// New unwrapped lines are added via CurrentLines.
|
|
|
|
// Usually points to \c &Lines. While parsing a preprocessor directive when
|
|
|
|
// there is an unfinished previous unwrapped line, will point to
|
|
|
|
// \c &PreprocessorDirectives.
|
|
|
|
std::vector<UnwrappedLine> *CurrentLines;
|
|
|
|
|
2013-01-23 17:32:48 +08:00
|
|
|
// We store for each line whether it must be a declaration depending on
|
|
|
|
// whether we are in a compound statement or not.
|
|
|
|
std::vector<bool> DeclarationScopeStack;
|
|
|
|
|
2013-01-10 23:05:09 +08:00
|
|
|
clang::DiagnosticsEngine &Diag;
|
2012-12-07 02:03:27 +08:00
|
|
|
const FormatStyle &Style;
|
2013-01-05 07:34:14 +08:00
|
|
|
FormatTokenSource *Tokens;
|
2012-12-04 02:12:45 +08:00
|
|
|
UnwrappedLineConsumer &Callback;
|
2013-01-10 19:52:21 +08:00
|
|
|
|
|
|
|
friend class ScopedLineState;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2013-01-07 21:26:07 +08:00
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-07 21:26:07 +08:00
|
|
|
#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
|