blob: 9c78d33632c6f935a961285e5851433575433690 [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>
Hans Wennborg2f65f7f2014-10-29 22:49:58 +000023#include <stack>
Daniel Jasper7c85fde2013-01-08 14:56:18 +000024
Daniel Jasperf7935112012-12-03 18:12:45 +000025namespace clang {
26namespace format {
27
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000028struct UnwrappedLineNode;
29
Daniel Jasperf7935112012-12-03 18:12:45 +000030/// \brief An unwrapped line is a sequence of \c Token, that we would like to
31/// put on a single line if there was no column limit.
32///
33/// This is used as a main interface between the \c UnwrappedLineParser and the
34/// \c UnwrappedLineFormatter. The key property is that changing the formatting
35/// within an unwrapped line does not affect any other unwrapped lines.
36struct UnwrappedLine {
Douglas Gregor7cfed212013-09-05 18:28:53 +000037 UnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +000038
Daniel Jaspera67a8f02013-01-16 10:41:46 +000039 // FIXME: Don't use std::list here.
Daniel Jasperdaffc0d2013-01-16 09:10:19 +000040 /// \brief The \c Tokens comprising this \c UnwrappedLine.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000041 std::list<UnwrappedLineNode> Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +000042
43 /// \brief The indent level of the \c UnwrappedLine.
44 unsigned Level;
Manuel Klimeka71e5d82013-01-02 16:30:12 +000045
46 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
47 bool InPPDirective;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000048
49 bool MustBeDeclaration;
Daniel Jasperf7935112012-12-03 18:12:45 +000050};
51
52class UnwrappedLineConsumer {
53public:
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000054 virtual ~UnwrappedLineConsumer() {}
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +000055 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Manuel Klimek71814b42013-10-11 21:25:45 +000056 virtual void finishRun() = 0;
Daniel Jasperf7935112012-12-03 18:12:45 +000057};
58
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000059class FormatTokenSource;
Alexander Kornienkoe3276842012-12-07 16:15:44 +000060
Daniel Jasperf7935112012-12-03 18:12:45 +000061class UnwrappedLineParser {
62public:
Daniel Jasperd0ec0d62014-11-04 12:41:02 +000063 UnwrappedLineParser(const FormatStyle &Style,
64 const AdditionalKeywords &Keywords,
65 ArrayRef<FormatToken *> Tokens,
Daniel Jasperf7935112012-12-03 18:12:45 +000066 UnwrappedLineConsumer &Callback);
67
Manuel Klimek20e0af62015-05-06 11:56:29 +000068 void parse();
Daniel Jasperf7935112012-12-03 18:12:45 +000069
70private:
Manuel Klimek71814b42013-10-11 21:25:45 +000071 void reset();
Manuel Klimek1a18c402013-04-12 14:13:36 +000072 void parseFile();
73 void parseLevel(bool HasOpeningBrace);
Manuel Klimekb212f3b2013-10-12 22:46:56 +000074 void parseBlock(bool MustBeDeclaration, bool AddLevel = true,
75 bool MunchSemi = true);
Manuel Klimek516e0542013-09-04 13:25:30 +000076 void parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +000077 void parsePPDirective();
Manuel Klimek1abf7892013-01-04 23:34:14 +000078 void parsePPDefine();
Manuel Klimek71814b42013-10-11 21:25:45 +000079 void parsePPIf(bool IfDef);
Alexander Kornienkof2e02122013-05-24 18:24:24 +000080 void parsePPElIf();
81 void parsePPElse();
82 void parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +000083 void parsePPUnknown();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +000084 void readTokenWithJavaScriptASI();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +000085 void parseStructuralElement();
Daniel Jasper3c883d12015-05-18 14:49:19 +000086 bool tryToParseBracedList();
Daniel Jasper015ed022013-09-13 09:20:45 +000087 bool parseBracedList(bool ContinueOnSemicolons = false);
Daniel Jasperf7935112012-12-03 18:12:45 +000088 void parseParens();
Daniel Jasperb88b25f2013-12-23 07:29:06 +000089 void parseSquare();
Daniel Jasperf7935112012-12-03 18:12:45 +000090 void parseIfThenElse();
Daniel Jasper04a71a42014-05-08 11:58:24 +000091 void parseTryCatch();
Alexander Kornienko37d6c942012-12-05 15:06:06 +000092 void parseForOrWhileLoop();
Daniel Jasperf7935112012-12-03 18:12:45 +000093 void parseDoWhile();
94 void parseLabel();
95 void parseCaseLabel();
96 void parseSwitch();
Alexander Kornienko578fdd82012-12-06 18:03:27 +000097 void parseNamespace();
Daniel Jasper6acf5132015-03-12 14:44:29 +000098 void parseNew();
Daniel Jasperf7935112012-12-03 18:12:45 +000099 void parseAccessSpecifier();
Daniel Jasper6f5a1932015-12-29 08:54:23 +0000100 bool parseEnum();
Daniel Jasper6be0f552014-11-13 15:56:28 +0000101 void parseJavaEnumBody();
Manuel Klimeke01bab52013-01-15 13:38:33 +0000102 void parseRecord();
Nico Weber8696a8d2013-01-09 21:15:03 +0000103 void parseObjCProtocolList();
104 void parseObjCUntilAtEnd();
Nico Weber2ce0ac52013-01-09 23:25:37 +0000105 void parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000106 void parseObjCProtocol();
Daniel Jasperfca735c2015-02-19 16:14:18 +0000107 void parseJavaScriptEs6ImportExport();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000108 bool tryToParseLambda();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000109 bool tryToParseLambdaIntroducer();
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000110 void tryToParseJSFunction();
Daniel Jasperf7935112012-12-03 18:12:45 +0000111 void addUnwrappedLine();
112 bool eof() const;
113 void nextToken();
Daniel Jasperb9a49902016-01-09 15:56:28 +0000114 const FormatToken *getPreviousToken();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000115 void readToken();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000116 void flushComments(bool NewlineBeforeNext);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000117 void pushToken(FormatToken *Tok);
Daniel Jasper3c883d12015-05-18 14:49:19 +0000118 void calculateBraceTypes(bool ExpectClassBody = false);
Manuel Klimek68b03042014-04-14 09:14:11 +0000119
120 // Marks a conditional compilation edge (for example, an '#if', '#ifdef',
121 // '#else' or merge conflict marker). If 'Unreachable' is true, assumes
122 // this branch either cannot be taken (for example '#if false'), or should
123 // not be taken in this round.
124 void conditionalCompilationCondition(bool Unreachable);
125 void conditionalCompilationStart(bool Unreachable);
126 void conditionalCompilationAlternative();
127 void conditionalCompilationEnd();
128
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000129 bool isOnNewLine(const FormatToken &FormatTok);
Manuel Klimekab419912013-05-23 09:41:43 +0000130
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000131 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
132 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
133 // and use that everywhere in the Parser.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000134 std::unique_ptr<UnwrappedLine> Line;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000135
136 // Comments are sorted into unwrapped lines by whether they are in the same
137 // line as the previous token, or not. If not, they belong to the next token.
138 // Since the next token might already be in a new unwrapped line, we need to
139 // store the comments belonging to that token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000140 SmallVector<FormatToken *, 1> CommentsBeforeNextToken;
141 FormatToken *FormatTok;
Manuel Klimek52b15152013-01-09 15:25:02 +0000142 bool MustBreakBeforeNextToken;
Daniel Jasperf7935112012-12-03 18:12:45 +0000143
Manuel Klimek05d82b72013-01-18 18:24:28 +0000144 // The parsed lines. Only added to through \c CurrentLines.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000145 SmallVector<UnwrappedLine, 8> Lines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000146
147 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
148 // Thus, we need to keep a list of preprocessor directives to be reported
149 // after an unwarpped line that has been started was finished.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000150 SmallVector<UnwrappedLine, 4> PreprocessorDirectives;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000151
152 // New unwrapped lines are added via CurrentLines.
153 // Usually points to \c &Lines. While parsing a preprocessor directive when
154 // there is an unfinished previous unwrapped line, will point to
155 // \c &PreprocessorDirectives.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000156 SmallVectorImpl<UnwrappedLine> *CurrentLines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000157
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000158 // We store for each line whether it must be a declaration depending on
159 // whether we are in a compound statement or not.
160 std::vector<bool> DeclarationScopeStack;
161
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000162 const FormatStyle &Style;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000163 const AdditionalKeywords &Keywords;
164
Manuel Klimek1abf7892013-01-04 23:34:14 +0000165 FormatTokenSource *Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +0000166 UnwrappedLineConsumer &Callback;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000167
Manuel Klimekab419912013-05-23 09:41:43 +0000168 // FIXME: This is a temporary measure until we have reworked the ownership
169 // of the format tokens. The goal is to have the actual tokens created and
170 // owned outside of and handed into the UnwrappedLineParser.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000171 ArrayRef<FormatToken *> AllTokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000172
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000173 // Represents preprocessor branch type, so we can find matching
174 // #if/#else/#endif directives.
175 enum PPBranchKind {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000176 PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
177 PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000178 };
179
180 // Keeps a stack of currently active preprocessor branching directives.
181 SmallVector<PPBranchKind, 16> PPStack;
182
Manuel Klimek71814b42013-10-11 21:25:45 +0000183 // The \c UnwrappedLineParser re-parses the code for each combination
184 // of preprocessor branches that can be taken.
185 // To that end, we take the same branch (#if, #else, or one of the #elif
186 // branches) for each nesting level of preprocessor branches.
187 // \c PPBranchLevel stores the current nesting level of preprocessor
188 // branches during one pass over the code.
189 int PPBranchLevel;
190
191 // Contains the current branch (#if, #else or one of the #elif branches)
192 // for each nesting level.
193 SmallVector<int, 8> PPLevelBranchIndex;
194
195 // Contains the maximum number of branches at each nesting level.
196 SmallVector<int, 8> PPLevelBranchCount;
197
198 // Contains the number of branches per nesting level we are currently
199 // in while parsing a preprocessor branch sequence.
200 // This is used to update PPLevelBranchCount at the end of a branch
201 // sequence.
202 std::stack<int> PPChainBranchIndex;
203
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000204 friend class ScopedLineState;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000205 friend class CompoundStatementIndenter;
Daniel Jasperf7935112012-12-03 18:12:45 +0000206};
207
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000208struct UnwrappedLineNode {
Craig Topper2145bc02014-05-09 08:15:10 +0000209 UnwrappedLineNode() : Tok(nullptr) {}
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000210 UnwrappedLineNode(FormatToken *Tok) : Tok(Tok) {}
211
212 FormatToken *Tok;
213 SmallVector<UnwrappedLine, 0> Children;
214};
215
Douglas Gregor7cfed212013-09-05 18:28:53 +0000216inline UnwrappedLine::UnwrappedLine()
217 : Level(0), InPPDirective(false), MustBeDeclaration(false) {}
218
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000219} // end namespace format
220} // end namespace clang
Daniel Jasperf7935112012-12-03 18:12:45 +0000221
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000222#endif