blob: a235960c7996bf1d9034e195964c94917ebe6511 [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
16#ifndef LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
17#define LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
18
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);
Manuel Klimek762dd182013-01-21 10:07:49 +000085 void parseReturn();
Daniel Jasperf7935112012-12-03 18:12:45 +000086 void parseParens();
Daniel Jasperb88b25f2013-12-23 07:29:06 +000087 void parseSquare();
Daniel Jasperf7935112012-12-03 18:12:45 +000088 void parseIfThenElse();
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 Jasperf7935112012-12-03 18:12:45 +0000104 void addUnwrappedLine();
105 bool eof() const;
106 void nextToken();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000107 void readToken();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000108 void flushComments(bool NewlineBeforeNext);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000109 void pushToken(FormatToken *Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000110 void calculateBraceTypes();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000111 void pushPPConditional();
Manuel Klimekab419912013-05-23 09:41:43 +0000112
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000113 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
114 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
115 // and use that everywhere in the Parser.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000116 OwningPtr<UnwrappedLine> Line;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000117
118 // Comments are sorted into unwrapped lines by whether they are in the same
119 // line as the previous token, or not. If not, they belong to the next token.
120 // Since the next token might already be in a new unwrapped line, we need to
121 // store the comments belonging to that token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000122 SmallVector<FormatToken *, 1> CommentsBeforeNextToken;
123 FormatToken *FormatTok;
Manuel Klimek52b15152013-01-09 15:25:02 +0000124 bool MustBreakBeforeNextToken;
Daniel Jasperf7935112012-12-03 18:12:45 +0000125
Manuel Klimek05d82b72013-01-18 18:24:28 +0000126 // The parsed lines. Only added to through \c CurrentLines.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000127 SmallVector<UnwrappedLine, 8> Lines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000128
129 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
130 // Thus, we need to keep a list of preprocessor directives to be reported
131 // after an unwarpped line that has been started was finished.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000132 SmallVector<UnwrappedLine, 4> PreprocessorDirectives;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000133
134 // New unwrapped lines are added via CurrentLines.
135 // Usually points to \c &Lines. While parsing a preprocessor directive when
136 // there is an unfinished previous unwrapped line, will point to
137 // \c &PreprocessorDirectives.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000138 SmallVectorImpl<UnwrappedLine> *CurrentLines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000139
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000140 // We store for each line whether it must be a declaration depending on
141 // whether we are in a compound statement or not.
142 std::vector<bool> DeclarationScopeStack;
143
Manuel Klimek1a18c402013-04-12 14:13:36 +0000144 // Will be true if we encounter an error that leads to possibily incorrect
145 // indentation levels.
146 bool StructuralError;
147
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000148 const FormatStyle &Style;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000149 FormatTokenSource *Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +0000150 UnwrappedLineConsumer &Callback;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000151
Manuel Klimekab419912013-05-23 09:41:43 +0000152 // FIXME: This is a temporary measure until we have reworked the ownership
153 // of the format tokens. The goal is to have the actual tokens created and
154 // owned outside of and handed into the UnwrappedLineParser.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000155 ArrayRef<FormatToken *> AllTokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000156
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000157 // Represents preprocessor branch type, so we can find matching
158 // #if/#else/#endif directives.
159 enum PPBranchKind {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000160 PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
161 PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000162 };
163
164 // Keeps a stack of currently active preprocessor branching directives.
165 SmallVector<PPBranchKind, 16> PPStack;
166
Manuel Klimek71814b42013-10-11 21:25:45 +0000167 // The \c UnwrappedLineParser re-parses the code for each combination
168 // of preprocessor branches that can be taken.
169 // To that end, we take the same branch (#if, #else, or one of the #elif
170 // branches) for each nesting level of preprocessor branches.
171 // \c PPBranchLevel stores the current nesting level of preprocessor
172 // branches during one pass over the code.
173 int PPBranchLevel;
174
175 // Contains the current branch (#if, #else or one of the #elif branches)
176 // for each nesting level.
177 SmallVector<int, 8> PPLevelBranchIndex;
178
179 // Contains the maximum number of branches at each nesting level.
180 SmallVector<int, 8> PPLevelBranchCount;
181
182 // Contains the number of branches per nesting level we are currently
183 // in while parsing a preprocessor branch sequence.
184 // This is used to update PPLevelBranchCount at the end of a branch
185 // sequence.
186 std::stack<int> PPChainBranchIndex;
187
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000188 friend class ScopedLineState;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000189 friend class CompoundStatementIndenter;
Daniel Jasperf7935112012-12-03 18:12:45 +0000190};
191
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000192struct UnwrappedLineNode {
193 UnwrappedLineNode() : Tok(NULL) {}
194 UnwrappedLineNode(FormatToken *Tok) : Tok(Tok) {}
195
196 FormatToken *Tok;
197 SmallVector<UnwrappedLine, 0> Children;
198};
199
Douglas Gregor7cfed212013-09-05 18:28:53 +0000200inline UnwrappedLine::UnwrappedLine()
201 : Level(0), InPPDirective(false), MustBeDeclaration(false) {}
202
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000203} // end namespace format
204} // end namespace clang
Daniel Jasperf7935112012-12-03 18:12:45 +0000205
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000206#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H