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.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
|
|
|
|
#define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "FormatToken.h"
|
2012-12-04 02:12:45 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2012-12-07 02:03:27 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2013-01-16 17:10:19 +08:00
|
|
|
#include <list>
|
2014-10-30 06:49:58 +08:00
|
|
|
#include <stack>
|
2013-01-08 22:56:18 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
struct UnwrappedLineNode;
|
|
|
|
|
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-09-06 02:28:53 +08:00
|
|
|
UnwrappedLine();
|
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.
|
2013-09-05 17:29:45 +08:00
|
|
|
std::list<UnwrappedLineNode> 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:
|
2015-10-20 21:23:58 +08:00
|
|
|
virtual ~UnwrappedLineConsumer() {}
|
2012-12-05 21:56:52 +08:00
|
|
|
virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
|
2013-10-12 05:25:45 +08:00
|
|
|
virtual void finishRun() = 0;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
class FormatTokenSource;
|
2012-12-08 00:15:44 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
class UnwrappedLineParser {
|
|
|
|
public:
|
2014-11-04 20:41:02 +08:00
|
|
|
UnwrappedLineParser(const FormatStyle &Style,
|
|
|
|
const AdditionalKeywords &Keywords,
|
|
|
|
ArrayRef<FormatToken *> Tokens,
|
2012-12-04 02:12:45 +08:00
|
|
|
UnwrappedLineConsumer &Callback);
|
|
|
|
|
2015-05-06 19:56:29 +08:00
|
|
|
void parse();
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
private:
|
2013-10-12 05:25:45 +08:00
|
|
|
void reset();
|
2013-04-12 22:13:36 +08:00
|
|
|
void parseFile();
|
|
|
|
void parseLevel(bool HasOpeningBrace);
|
2013-10-13 06:46:56 +08:00
|
|
|
void parseBlock(bool MustBeDeclaration, bool AddLevel = true,
|
|
|
|
bool MunchSemi = true);
|
2013-09-04 21:25:30 +08:00
|
|
|
void parseChildBlock();
|
2012-12-04 02:12:45 +08:00
|
|
|
void parsePPDirective();
|
2013-01-05 07:34:14 +08:00
|
|
|
void parsePPDefine();
|
2013-10-12 05:25:45 +08:00
|
|
|
void parsePPIf(bool IfDef);
|
2013-05-25 02:24:24 +08:00
|
|
|
void parsePPElIf();
|
|
|
|
void parsePPElse();
|
|
|
|
void parsePPEndIf();
|
2013-01-05 07:34:14 +08:00
|
|
|
void parsePPUnknown();
|
2016-03-15 03:21:36 +08:00
|
|
|
void readTokenWithJavaScriptASI();
|
2013-01-07 22:56:16 +08:00
|
|
|
void parseStructuralElement();
|
2015-05-18 22:49:19 +08:00
|
|
|
bool tryToParseBracedList();
|
2013-09-13 17:20:45 +08:00
|
|
|
bool parseBracedList(bool ContinueOnSemicolons = false);
|
2012-12-04 02:12:45 +08:00
|
|
|
void parseParens();
|
2013-12-23 15:29:06 +08:00
|
|
|
void parseSquare();
|
2012-12-04 02:12:45 +08:00
|
|
|
void parseIfThenElse();
|
2014-05-08 19:58:24 +08:00
|
|
|
void parseTryCatch();
|
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();
|
2015-03-12 22:44:29 +08:00
|
|
|
void parseNew();
|
2012-12-04 02:12:45 +08:00
|
|
|
void parseAccessSpecifier();
|
2015-12-29 16:54:23 +08:00
|
|
|
bool parseEnum();
|
2014-11-13 23:56:28 +08:00
|
|
|
void parseJavaEnumBody();
|
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();
|
2015-02-20 00:14:18 +08:00
|
|
|
void parseJavaScriptEs6ImportExport();
|
2013-12-23 15:29:06 +08:00
|
|
|
bool tryToParseLambda();
|
2013-09-03 23:10:01 +08:00
|
|
|
bool tryToParseLambdaIntroducer();
|
2014-05-08 17:25:39 +08:00
|
|
|
void tryToParseJSFunction();
|
2012-12-04 02:12:45 +08:00
|
|
|
void addUnwrappedLine();
|
|
|
|
bool eof() const;
|
|
|
|
void nextToken();
|
2016-01-09 23:56:28 +08:00
|
|
|
const FormatToken *getPreviousToken();
|
2013-01-05 07:34:14 +08:00
|
|
|
void readToken();
|
2013-01-23 00:31:55 +08:00
|
|
|
void flushComments(bool NewlineBeforeNext);
|
2013-05-28 19:55:06 +08:00
|
|
|
void pushToken(FormatToken *Tok);
|
2015-05-18 22:49:19 +08:00
|
|
|
void calculateBraceTypes(bool ExpectClassBody = false);
|
2014-04-14 17:14:11 +08:00
|
|
|
|
|
|
|
// Marks a conditional compilation edge (for example, an '#if', '#ifdef',
|
|
|
|
// '#else' or merge conflict marker). If 'Unreachable' is true, assumes
|
|
|
|
// this branch either cannot be taken (for example '#if false'), or should
|
|
|
|
// not be taken in this round.
|
|
|
|
void conditionalCompilationCondition(bool Unreachable);
|
|
|
|
void conditionalCompilationStart(bool Unreachable);
|
|
|
|
void conditionalCompilationAlternative();
|
|
|
|
void conditionalCompilationEnd();
|
|
|
|
|
2014-05-09 21:11:16 +08:00
|
|
|
bool isOnNewLine(const FormatToken &FormatTok);
|
2013-05-23 17:41:43 +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.
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<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.
|
2013-05-28 19:55:06 +08:00
|
|
|
SmallVector<FormatToken *, 1> CommentsBeforeNextToken;
|
|
|
|
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-09-05 17:29:45 +08:00
|
|
|
SmallVector<UnwrappedLine, 8> Lines;
|
2013-01-18 22:04:34 +08:00
|
|
|
|
|
|
|
// 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.
|
2013-09-05 17:29:45 +08:00
|
|
|
SmallVector<UnwrappedLine, 4> PreprocessorDirectives;
|
2013-01-18 22:04:34 +08:00
|
|
|
|
|
|
|
// 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.
|
2013-09-05 17:29:45 +08:00
|
|
|
SmallVectorImpl<UnwrappedLine> *CurrentLines;
|
2013-01-18 22:04:34 +08:00
|
|
|
|
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;
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
const FormatStyle &Style;
|
2014-11-04 20:41:02 +08:00
|
|
|
const AdditionalKeywords &Keywords;
|
|
|
|
|
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
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
// FIXME: This is a temporary measure until we have reworked the ownership
|
|
|
|
// of the format tokens. The goal is to have the actual tokens created and
|
|
|
|
// owned outside of and handed into the UnwrappedLineParser.
|
2013-05-28 19:55:06 +08:00
|
|
|
ArrayRef<FormatToken *> AllTokens;
|
2013-05-23 17:41:43 +08:00
|
|
|
|
2013-05-25 02:24:24 +08:00
|
|
|
// Represents preprocessor branch type, so we can find matching
|
|
|
|
// #if/#else/#endif directives.
|
|
|
|
enum PPBranchKind {
|
2013-07-08 22:34:09 +08:00
|
|
|
PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
|
|
|
|
PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0
|
2013-05-25 02:24:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Keeps a stack of currently active preprocessor branching directives.
|
|
|
|
SmallVector<PPBranchKind, 16> PPStack;
|
|
|
|
|
2013-10-12 05:25:45 +08:00
|
|
|
// The \c UnwrappedLineParser re-parses the code for each combination
|
|
|
|
// of preprocessor branches that can be taken.
|
|
|
|
// To that end, we take the same branch (#if, #else, or one of the #elif
|
|
|
|
// branches) for each nesting level of preprocessor branches.
|
|
|
|
// \c PPBranchLevel stores the current nesting level of preprocessor
|
|
|
|
// branches during one pass over the code.
|
|
|
|
int PPBranchLevel;
|
|
|
|
|
|
|
|
// Contains the current branch (#if, #else or one of the #elif branches)
|
|
|
|
// for each nesting level.
|
|
|
|
SmallVector<int, 8> PPLevelBranchIndex;
|
|
|
|
|
|
|
|
// Contains the maximum number of branches at each nesting level.
|
|
|
|
SmallVector<int, 8> PPLevelBranchCount;
|
|
|
|
|
|
|
|
// Contains the number of branches per nesting level we are currently
|
|
|
|
// in while parsing a preprocessor branch sequence.
|
|
|
|
// This is used to update PPLevelBranchCount at the end of a branch
|
|
|
|
// sequence.
|
|
|
|
std::stack<int> PPChainBranchIndex;
|
|
|
|
|
2013-01-10 19:52:21 +08:00
|
|
|
friend class ScopedLineState;
|
2013-12-12 17:49:52 +08:00
|
|
|
friend class CompoundStatementIndenter;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
struct UnwrappedLineNode {
|
2014-05-09 16:15:10 +08:00
|
|
|
UnwrappedLineNode() : Tok(nullptr) {}
|
2013-09-05 17:29:45 +08:00
|
|
|
UnwrappedLineNode(FormatToken *Tok) : Tok(Tok) {}
|
|
|
|
|
|
|
|
FormatToken *Tok;
|
|
|
|
SmallVector<UnwrappedLine, 0> Children;
|
|
|
|
};
|
|
|
|
|
2013-09-06 02:28:53 +08:00
|
|
|
inline UnwrappedLine::UnwrappedLine()
|
|
|
|
: Level(0), InPPDirective(false), MustBeDeclaration(false) {}
|
|
|
|
|
2013-01-07 21:26:07 +08:00
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#endif
|