blob: be13fa5cfc893dc05294eb8771e9268c58d1c01a [file] [log] [blame]
Chandler Carruth3a022472012-12-04 09:13:33 +00001//===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===//
Daniel Jasperf7935112012-12-03 18:12:45 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains the declaration of the UnwrappedLineParser,
12/// which turns a stream of tokens into UnwrappedLines.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000016#ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
17#define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
Daniel Jasperf7935112012-12-03 18:12:45 +000018
Chandler Carruth5553d0d2014-01-07 11:51:46 +000019#include "FormatToken.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000020#include "clang/Basic/IdentifierTable.h"
Alexander Kornienko578fdd82012-12-06 18:03:27 +000021#include "clang/Format/Format.h"
Daniel Jasperdaffc0d2013-01-16 09:10:19 +000022#include <list>
Daniel Jasper7c85fde2013-01-08 14:56:18 +000023
Daniel Jasperf7935112012-12-03 18:12:45 +000024namespace clang {
25namespace format {
26
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000027struct UnwrappedLineNode;
28
Daniel Jasperf7935112012-12-03 18:12:45 +000029/// \brief An unwrapped line is a sequence of \c Token, that we would like to
30/// put on a single line if there was no column limit.
31///
32/// This is used as a main interface between the \c UnwrappedLineParser and the
33/// \c UnwrappedLineFormatter. The key property is that changing the formatting
34/// within an unwrapped line does not affect any other unwrapped lines.
35struct UnwrappedLine {
Douglas Gregor7cfed212013-09-05 18:28:53 +000036 UnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +000037
Daniel Jaspera67a8f02013-01-16 10:41:46 +000038 // FIXME: Don't use std::list here.
Daniel Jasperdaffc0d2013-01-16 09:10:19 +000039 /// \brief The \c Tokens comprising this \c UnwrappedLine.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000040 std::list<UnwrappedLineNode> Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +000041
42 /// \brief The indent level of the \c UnwrappedLine.
43 unsigned Level;
Manuel Klimeka71e5d82013-01-02 16:30:12 +000044
45 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
46 bool InPPDirective;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000047
48 bool MustBeDeclaration;
Daniel Jasperf7935112012-12-03 18:12:45 +000049};
50
51class UnwrappedLineConsumer {
52public:
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000053 virtual ~UnwrappedLineConsumer() {}
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +000054 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Manuel Klimek71814b42013-10-11 21:25:45 +000055 virtual void finishRun() = 0;
Daniel Jasperf7935112012-12-03 18:12:45 +000056};
57
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000058class FormatTokenSource;
Alexander Kornienkoe3276842012-12-07 16:15:44 +000059
Daniel Jasperf7935112012-12-03 18:12:45 +000060class UnwrappedLineParser {
61public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000062 UnwrappedLineParser(const FormatStyle &Style, ArrayRef<FormatToken *> Tokens,
Daniel Jasperf7935112012-12-03 18:12:45 +000063 UnwrappedLineConsumer &Callback);
64
Alexander Kornienko870f9eb2012-12-04 17:27:50 +000065 /// Returns true in case of a structural error.
66 bool parse();
Daniel Jasperf7935112012-12-03 18:12:45 +000067
68private:
Manuel Klimek71814b42013-10-11 21:25:45 +000069 void reset();
Manuel Klimek1a18c402013-04-12 14:13:36 +000070 void parseFile();
71 void parseLevel(bool HasOpeningBrace);
Manuel Klimekb212f3b2013-10-12 22:46:56 +000072 void parseBlock(bool MustBeDeclaration, bool AddLevel = true,
73 bool MunchSemi = true);
Manuel Klimek516e0542013-09-04 13:25:30 +000074 void parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +000075 void parsePPDirective();
Manuel Klimek1abf7892013-01-04 23:34:14 +000076 void parsePPDefine();
Manuel Klimek71814b42013-10-11 21:25:45 +000077 void parsePPIf(bool IfDef);
Alexander Kornienkof2e02122013-05-24 18:24:24 +000078 void parsePPElIf();
79 void parsePPElse();
80 void parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +000081 void parsePPUnknown();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +000082 void parseStructuralElement();
Manuel Klimekab419912013-05-23 09:41:43 +000083 bool tryToParseBracedList();
Daniel Jasper015ed022013-09-13 09:20:45 +000084 bool parseBracedList(bool ContinueOnSemicolons = false);
Daniel Jasperf7935112012-12-03 18:12:45 +000085 void parseParens();
Daniel Jasperb88b25f2013-12-23 07:29:06 +000086 void parseSquare();
Daniel Jasperf7935112012-12-03 18:12:45 +000087 void parseIfThenElse();
Daniel Jasper04a71a42014-05-08 11:58:24 +000088 void parseTryCatch();
Alexander Kornienko37d6c942012-12-05 15:06:06 +000089 void parseForOrWhileLoop();
Daniel Jasperf7935112012-12-03 18:12:45 +000090 void parseDoWhile();
91 void parseLabel();
92 void parseCaseLabel();
93 void parseSwitch();
Alexander Kornienko578fdd82012-12-06 18:03:27 +000094 void parseNamespace();
Daniel Jasperf7935112012-12-03 18:12:45 +000095 void parseAccessSpecifier();
96 void parseEnum();
Manuel Klimeke01bab52013-01-15 13:38:33 +000097 void parseRecord();
Nico Weber8696a8d2013-01-09 21:15:03 +000098 void parseObjCProtocolList();
99 void parseObjCUntilAtEnd();
Nico Weber2ce0ac52013-01-09 23:25:37 +0000100 void parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000101 void parseObjCProtocol();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000102 bool tryToParseLambda();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000103 bool tryToParseLambdaIntroducer();
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000104 void tryToParseJSFunction();
Daniel Jasperf7935112012-12-03 18:12:45 +0000105 void addUnwrappedLine();
106 bool eof() const;
107 void nextToken();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000108 void readToken();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000109 void flushComments(bool NewlineBeforeNext);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000110 void pushToken(FormatToken *Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000111 void calculateBraceTypes();
Manuel Klimek68b03042014-04-14 09:14:11 +0000112
113 // Marks a conditional compilation edge (for example, an '#if', '#ifdef',
114 // '#else' or merge conflict marker). If 'Unreachable' is true, assumes
115 // this branch either cannot be taken (for example '#if false'), or should
116 // not be taken in this round.
117 void conditionalCompilationCondition(bool Unreachable);
118 void conditionalCompilationStart(bool Unreachable);
119 void conditionalCompilationAlternative();
120 void conditionalCompilationEnd();
121
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000122 bool isOnNewLine(const FormatToken &FormatTok);
Manuel Klimekab419912013-05-23 09:41:43 +0000123
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000124 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
125 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
126 // and use that everywhere in the Parser.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000127 std::unique_ptr<UnwrappedLine> Line;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000128
129 // Comments are sorted into unwrapped lines by whether they are in the same
130 // line as the previous token, or not. If not, they belong to the next token.
131 // Since the next token might already be in a new unwrapped line, we need to
132 // store the comments belonging to that token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000133 SmallVector<FormatToken *, 1> CommentsBeforeNextToken;
134 FormatToken *FormatTok;
Manuel Klimek52b15152013-01-09 15:25:02 +0000135 bool MustBreakBeforeNextToken;
Daniel Jasperf7935112012-12-03 18:12:45 +0000136
Manuel Klimek05d82b72013-01-18 18:24:28 +0000137 // The parsed lines. Only added to through \c CurrentLines.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000138 SmallVector<UnwrappedLine, 8> Lines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000139
140 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
141 // Thus, we need to keep a list of preprocessor directives to be reported
142 // after an unwarpped line that has been started was finished.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000143 SmallVector<UnwrappedLine, 4> PreprocessorDirectives;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000144
145 // New unwrapped lines are added via CurrentLines.
146 // Usually points to \c &Lines. While parsing a preprocessor directive when
147 // there is an unfinished previous unwrapped line, will point to
148 // \c &PreprocessorDirectives.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000149 SmallVectorImpl<UnwrappedLine> *CurrentLines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000150
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000151 // We store for each line whether it must be a declaration depending on
152 // whether we are in a compound statement or not.
153 std::vector<bool> DeclarationScopeStack;
154
Manuel Klimek1a18c402013-04-12 14:13:36 +0000155 // Will be true if we encounter an error that leads to possibily incorrect
156 // indentation levels.
157 bool StructuralError;
158
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000159 const FormatStyle &Style;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000160 FormatTokenSource *Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +0000161 UnwrappedLineConsumer &Callback;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000162
Manuel Klimekab419912013-05-23 09:41:43 +0000163 // FIXME: This is a temporary measure until we have reworked the ownership
164 // of the format tokens. The goal is to have the actual tokens created and
165 // owned outside of and handed into the UnwrappedLineParser.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000166 ArrayRef<FormatToken *> AllTokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000167
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000168 // Represents preprocessor branch type, so we can find matching
169 // #if/#else/#endif directives.
170 enum PPBranchKind {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000171 PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
172 PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000173 };
174
175 // Keeps a stack of currently active preprocessor branching directives.
176 SmallVector<PPBranchKind, 16> PPStack;
177
Manuel Klimek71814b42013-10-11 21:25:45 +0000178 // The \c UnwrappedLineParser re-parses the code for each combination
179 // of preprocessor branches that can be taken.
180 // To that end, we take the same branch (#if, #else, or one of the #elif
181 // branches) for each nesting level of preprocessor branches.
182 // \c PPBranchLevel stores the current nesting level of preprocessor
183 // branches during one pass over the code.
184 int PPBranchLevel;
185
186 // Contains the current branch (#if, #else or one of the #elif branches)
187 // for each nesting level.
188 SmallVector<int, 8> PPLevelBranchIndex;
189
190 // Contains the maximum number of branches at each nesting level.
191 SmallVector<int, 8> PPLevelBranchCount;
192
193 // Contains the number of branches per nesting level we are currently
194 // in while parsing a preprocessor branch sequence.
195 // This is used to update PPLevelBranchCount at the end of a branch
196 // sequence.
197 std::stack<int> PPChainBranchIndex;
198
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000199 friend class ScopedLineState;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000200 friend class CompoundStatementIndenter;
Daniel Jasperf7935112012-12-03 18:12:45 +0000201};
202
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000203struct UnwrappedLineNode {
Craig Topper2145bc02014-05-09 08:15:10 +0000204 UnwrappedLineNode() : Tok(nullptr) {}
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000205 UnwrappedLineNode(FormatToken *Tok) : Tok(Tok) {}
206
207 FormatToken *Tok;
208 SmallVector<UnwrappedLine, 0> Children;
209};
210
Douglas Gregor7cfed212013-09-05 18:28:53 +0000211inline UnwrappedLine::UnwrappedLine()
212 : Level(0), InPPDirective(false), MustBeDeclaration(false) {}
213
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000214} // end namespace format
215} // end namespace clang
Daniel Jasperf7935112012-12-03 18:12:45 +0000216
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000217#endif