blob: 8e925597422f36696107ef513c91737983217ccd [file] [log] [blame]
Chandler Carruth55fc8732012-12-04 09:13:33 +00001//===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===//
Daniel Jasperbac016b2012-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 Jasperbac016b2012-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
Daniel Jasperbac016b2012-12-03 18:12:45 +000019#include "clang/Basic/IdentifierTable.h"
Alexander Kornienko15757312012-12-06 18:03:27 +000020#include "clang/Format/Format.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000021#include "clang/Lex/Lexer.h"
Daniel Jaspercbb6c412013-01-16 09:10:19 +000022#include <list>
Daniel Jasper26f7e782013-01-08 14:56:18 +000023
Daniel Jasperbac016b2012-12-03 18:12:45 +000024namespace clang {
25namespace format {
26
27/// \brief A wrapper around a \c Token storing information about the
28/// whitespace characters preceeding it.
29struct FormatToken {
Manuel Klimeka080a182013-01-02 16:30:12 +000030 FormatToken()
Manuel Klimekde008c02013-05-27 15:23:34 +000031 : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
32 TokenLength(0), IsFirst(false), MustBreakBefore(false) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +000033
34 /// \brief The \c Token.
35 Token Tok;
36
37 /// \brief The number of newlines immediately before the \c Token.
38 ///
39 /// This can be used to determine what the user wrote in the original code
40 /// and thereby e.g. leave an empty line between two function definitions.
41 unsigned NewlinesBefore;
42
Manuel Klimeka080a182013-01-02 16:30:12 +000043 /// \brief Whether there is at least one unescaped newline before the \c
44 /// Token.
45 bool HasUnescapedNewline;
46
Manuel Klimekad3094b2013-05-23 10:56:37 +000047 /// \brief The range of the whitespace immediately preceeding the \c Token.
48 SourceRange WhitespaceRange;
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000049
Daniel Jasper1eee6c42013-03-04 13:43:19 +000050 /// \brief The offset just past the last '\n' in this token's leading
51 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
52 unsigned LastNewlineOffset;
53
Manuel Klimek95419382013-01-07 07:56:50 +000054 /// \brief The length of the non-whitespace parts of the token. This is
55 /// necessary because we need to handle escaped newlines that are stored
56 /// with the token.
57 unsigned TokenLength;
58
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000059 /// \brief Indicates that this is the first token.
60 bool IsFirst;
Daniel Jasper26f7e782013-01-08 14:56:18 +000061
Manuel Klimek526ed112013-01-09 15:25:02 +000062 /// \brief Whether there must be a line break before this token.
63 ///
64 /// This happens for example when a preprocessor directive ended directly
65 /// before the token.
66 bool MustBreakBefore;
Alexander Kornienko919398b2013-04-17 17:34:05 +000067
Alexander Kornienko919398b2013-04-17 17:34:05 +000068 /// \brief Returns actual token start location without leading escaped
69 /// newlines and whitespace.
70 ///
71 /// This can be different to Tok.getLocation(), which includes leading escaped
72 /// newlines.
73 SourceLocation getStartOfNonWhitespace() const {
Manuel Klimekad3094b2013-05-23 10:56:37 +000074 return WhitespaceRange.getEnd();
Alexander Kornienko919398b2013-04-17 17:34:05 +000075 }
Manuel Klimekde008c02013-05-27 15:23:34 +000076
77 /// \brief The raw text of the token.
78 ///
79 /// Contains the raw token text without leading whitespace and without leading
80 /// escaped newlines.
81 StringRef TokenText;
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +000082
83private:
84 // Disallow copying.
85 FormatToken(const FormatToken &);
86 void operator=(const FormatToken &);
Daniel Jasperbac016b2012-12-03 18:12:45 +000087};
88
89/// \brief An unwrapped line is a sequence of \c Token, that we would like to
90/// put on a single line if there was no column limit.
91///
92/// This is used as a main interface between the \c UnwrappedLineParser and the
93/// \c UnwrappedLineFormatter. The key property is that changing the formatting
94/// within an unwrapped line does not affect any other unwrapped lines.
95struct UnwrappedLine {
Manuel Klimek70b03f42013-01-23 09:32:48 +000096 UnwrappedLine() : Level(0), InPPDirective(false), MustBeDeclaration(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000097 }
98
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +000099 // FIXME: Don't use std::list here.
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000100 /// \brief The \c Tokens comprising this \c UnwrappedLine.
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +0000101 std::list<FormatToken *> Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102
103 /// \brief The indent level of the \c UnwrappedLine.
104 unsigned Level;
Manuel Klimeka080a182013-01-02 16:30:12 +0000105
106 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
107 bool InPPDirective;
Manuel Klimek70b03f42013-01-23 09:32:48 +0000108
109 bool MustBeDeclaration;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000110};
111
112class UnwrappedLineConsumer {
113public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +0000114 virtual ~UnwrappedLineConsumer() {
115 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +0000116 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000117};
118
Manuel Klimek96e888b2013-05-28 11:55:06 +0000119class FormatTokenSource;
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000120
Daniel Jasperbac016b2012-12-03 18:12:45 +0000121class UnwrappedLineParser {
122public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000123 UnwrappedLineParser(const FormatStyle &Style, ArrayRef<FormatToken *> Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +0000124 UnwrappedLineConsumer &Callback);
125
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000126 /// Returns true in case of a structural error.
127 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000128
129private:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000130 void parseFile();
131 void parseLevel(bool HasOpeningBrace);
132 void parseBlock(bool MustBeDeclaration, unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000133 void parsePPDirective();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000134 void parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000135 void parsePPIf();
136 void parsePPIfdef();
137 void parsePPElIf();
138 void parsePPElse();
139 void parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000140 void parsePPUnknown();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000141 void parseStructuralElement();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000142 bool tryToParseBracedList();
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000143 void parseBracedList();
Manuel Klimekc44ee892013-01-21 10:07:49 +0000144 void parseReturn();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000145 void parseParens();
146 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000147 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000148 void parseDoWhile();
149 void parseLabel();
150 void parseCaseLabel();
151 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000152 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000153 void parseAccessSpecifier();
154 void parseEnum();
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000155 void parseRecord();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000156 void parseObjCProtocolList();
157 void parseObjCUntilAtEnd();
Nico Weber50767d82013-01-09 23:25:37 +0000158 void parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000159 void parseObjCProtocol();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000160 void addUnwrappedLine();
161 bool eof() const;
162 void nextToken();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000163 void readToken();
Manuel Klimek86721d22013-01-22 16:31:55 +0000164 void flushComments(bool NewlineBeforeNext);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000165 void pushToken(FormatToken *Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000166 void calculateBraceTypes();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000167 void pushPPConditional();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000168
169 // Represents what type of block a left brace opens.
170 enum LBraceState {
171 BS_Unknown,
172 BS_Block,
173 BS_BracedInit
174 };
Daniel Jasperbac016b2012-12-03 18:12:45 +0000175
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000176 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
177 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
178 // and use that everywhere in the Parser.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000179 OwningPtr<UnwrappedLine> Line;
Manuel Klimek86721d22013-01-22 16:31:55 +0000180
181 // Comments are sorted into unwrapped lines by whether they are in the same
182 // line as the previous token, or not. If not, they belong to the next token.
183 // Since the next token might already be in a new unwrapped line, we need to
184 // store the comments belonging to that token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000185 SmallVector<FormatToken *, 1> CommentsBeforeNextToken;
186 FormatToken *FormatTok;
Manuel Klimek526ed112013-01-09 15:25:02 +0000187 bool MustBreakBeforeNextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000188
Manuel Klimekba287dc2013-01-18 18:24:28 +0000189 // The parsed lines. Only added to through \c CurrentLines.
Manuel Klimek525fe162013-01-18 14:04:34 +0000190 std::vector<UnwrappedLine> Lines;
191
192 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
193 // Thus, we need to keep a list of preprocessor directives to be reported
194 // after an unwarpped line that has been started was finished.
195 std::vector<UnwrappedLine> PreprocessorDirectives;
196
197 // New unwrapped lines are added via CurrentLines.
198 // Usually points to \c &Lines. While parsing a preprocessor directive when
199 // there is an unfinished previous unwrapped line, will point to
200 // \c &PreprocessorDirectives.
201 std::vector<UnwrappedLine> *CurrentLines;
202
Manuel Klimek70b03f42013-01-23 09:32:48 +0000203 // We store for each line whether it must be a declaration depending on
204 // whether we are in a compound statement or not.
205 std::vector<bool> DeclarationScopeStack;
206
Manuel Klimek67d080d2013-04-12 14:13:36 +0000207 // Will be true if we encounter an error that leads to possibily incorrect
208 // indentation levels.
209 bool StructuralError;
210
Alexander Kornienko15757312012-12-06 18:03:27 +0000211 const FormatStyle &Style;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000212 FormatTokenSource *Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000213 UnwrappedLineConsumer &Callback;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000214
Manuel Klimek80829bd2013-05-23 09:41:43 +0000215 // FIXME: This is a temporary measure until we have reworked the ownership
216 // of the format tokens. The goal is to have the actual tokens created and
217 // owned outside of and handed into the UnwrappedLineParser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000218 ArrayRef<FormatToken *> AllTokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000219
220 // FIXME: Currently we cannot store attributes with tokens, as we treat
221 // them as read-only; thus, we now store the brace state indexed by the
222 // position of the token in the stream (see \c AllTokens).
223 SmallVector<LBraceState, 16> LBraces;
224
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000225 // Represents preprocessor branch type, so we can find matching
226 // #if/#else/#endif directives.
227 enum PPBranchKind {
228 PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
229 PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0
230 };
231
232 // Keeps a stack of currently active preprocessor branching directives.
233 SmallVector<PPBranchKind, 16> PPStack;
234
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000235 friend class ScopedLineState;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000236};
237
Daniel Jaspercd162382013-01-07 13:26:07 +0000238} // end namespace format
239} // end namespace clang
Daniel Jasperbac016b2012-12-03 18:12:45 +0000240
Daniel Jaspercd162382013-01-07 13:26:07 +0000241#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H