blob: 842b1086460a6475fe786dac596afc793541a04a [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/Basic/SourceManager.h"
Alexander Kornienko15757312012-12-06 18:03:27 +000021#include "clang/Format/Format.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000022#include "clang/Lex/Lexer.h"
Daniel Jaspercbb6c412013-01-16 09:10:19 +000023#include <list>
Daniel Jasper26f7e782013-01-08 14:56:18 +000024
Daniel Jasperbac016b2012-12-03 18:12:45 +000025namespace clang {
26namespace format {
27
28/// \brief A wrapper around a \c Token storing information about the
29/// whitespace characters preceeding it.
30struct FormatToken {
Manuel Klimeka080a182013-01-02 16:30:12 +000031 FormatToken()
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000032 : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
Daniel Jasper1eee6c42013-03-04 13:43:19 +000033 LastNewlineOffset(0), TokenLength(0), IsFirst(false),
Alexander Kornienko919398b2013-04-17 17:34:05 +000034 MustBreakBefore(false), TrailingWhiteSpaceLength(0) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +000035
36 /// \brief The \c Token.
37 Token Tok;
38
39 /// \brief The number of newlines immediately before the \c Token.
40 ///
41 /// This can be used to determine what the user wrote in the original code
42 /// and thereby e.g. leave an empty line between two function definitions.
43 unsigned NewlinesBefore;
44
Manuel Klimeka080a182013-01-02 16:30:12 +000045 /// \brief Whether there is at least one unescaped newline before the \c
46 /// Token.
47 bool HasUnescapedNewline;
48
Daniel Jasperbac016b2012-12-03 18:12:45 +000049 /// \brief The location of the start of the whitespace immediately preceeding
50 /// the \c Token.
51 ///
52 /// Used together with \c WhiteSpaceLength to create a \c Replacement.
53 SourceLocation WhiteSpaceStart;
54
55 /// \brief The length in characters of the whitespace immediately preceeding
56 /// the \c Token.
57 unsigned WhiteSpaceLength;
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000058
Daniel Jasper1eee6c42013-03-04 13:43:19 +000059 /// \brief The offset just past the last '\n' in this token's leading
60 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
61 unsigned LastNewlineOffset;
62
Manuel Klimek95419382013-01-07 07:56:50 +000063 /// \brief The length of the non-whitespace parts of the token. This is
64 /// necessary because we need to handle escaped newlines that are stored
65 /// with the token.
66 unsigned TokenLength;
67
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000068 /// \brief Indicates that this is the first token.
69 bool IsFirst;
Daniel Jasper26f7e782013-01-08 14:56:18 +000070
Manuel Klimek526ed112013-01-09 15:25:02 +000071 /// \brief Whether there must be a line break before this token.
72 ///
73 /// This happens for example when a preprocessor directive ended directly
74 /// before the token.
75 bool MustBreakBefore;
Alexander Kornienko919398b2013-04-17 17:34:05 +000076
77 /// \brief Number of characters of trailing whitespace.
78 unsigned TrailingWhiteSpaceLength;
79
80 /// \brief Returns actual token start location without leading escaped
81 /// newlines and whitespace.
82 ///
83 /// This can be different to Tok.getLocation(), which includes leading escaped
84 /// newlines.
85 SourceLocation getStartOfNonWhitespace() const {
86 return WhiteSpaceStart.getLocWithOffset(WhiteSpaceLength);
87 }
Daniel Jasperbac016b2012-12-03 18:12:45 +000088};
89
90/// \brief An unwrapped line is a sequence of \c Token, that we would like to
91/// put on a single line if there was no column limit.
92///
93/// This is used as a main interface between the \c UnwrappedLineParser and the
94/// \c UnwrappedLineFormatter. The key property is that changing the formatting
95/// within an unwrapped line does not affect any other unwrapped lines.
96struct UnwrappedLine {
Manuel Klimek70b03f42013-01-23 09:32:48 +000097 UnwrappedLine() : Level(0), InPPDirective(false), MustBeDeclaration(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000098 }
99
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +0000100 // FIXME: Don't use std::list here.
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000101 /// \brief The \c Tokens comprising this \c UnwrappedLine.
102 std::list<FormatToken> Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000103
104 /// \brief The indent level of the \c UnwrappedLine.
105 unsigned Level;
Manuel Klimeka080a182013-01-02 16:30:12 +0000106
107 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
108 bool InPPDirective;
Manuel Klimek70b03f42013-01-23 09:32:48 +0000109
110 bool MustBeDeclaration;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000111};
112
113class UnwrappedLineConsumer {
114public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +0000115 virtual ~UnwrappedLineConsumer() {
116 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +0000117 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000118};
119
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000120class FormatTokenSource {
121public:
Matt Beaumont-Gay422daa12012-12-07 22:49:27 +0000122 virtual ~FormatTokenSource() {
123 }
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000124 virtual FormatToken getNextToken() = 0;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000125
126 // FIXME: This interface will become an implementation detail of
127 // the UnwrappedLineParser once we switch to generate all tokens
128 // up-front.
129 virtual unsigned getPosition() { return 0; }
130 virtual FormatToken setPosition(unsigned Position) { assert(false); }
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000131};
132
Daniel Jasperbac016b2012-12-03 18:12:45 +0000133class UnwrappedLineParser {
134public:
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000135 UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +0000136 UnwrappedLineConsumer &Callback);
137
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000138 /// Returns true in case of a structural error.
139 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000140
141private:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000142 void parseFile();
143 void parseLevel(bool HasOpeningBrace);
144 void parseBlock(bool MustBeDeclaration, unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000145 void parsePPDirective();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000146 void parsePPDefine();
147 void parsePPUnknown();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000148 void parseStructuralElement();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000149 bool tryToParseBracedList();
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000150 void parseBracedList();
Manuel Klimekc44ee892013-01-21 10:07:49 +0000151 void parseReturn();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000152 void parseParens();
153 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000154 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000155 void parseDoWhile();
156 void parseLabel();
157 void parseCaseLabel();
158 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000159 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000160 void parseAccessSpecifier();
161 void parseEnum();
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000162 void parseRecord();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000163 void parseObjCProtocolList();
164 void parseObjCUntilAtEnd();
Nico Weber50767d82013-01-09 23:25:37 +0000165 void parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000166 void parseObjCProtocol();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000167 void addUnwrappedLine();
168 bool eof() const;
169 void nextToken();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000170 void readToken();
Manuel Klimek86721d22013-01-22 16:31:55 +0000171 void flushComments(bool NewlineBeforeNext);
172 void pushToken(const FormatToken &Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000173 void calculateBraceTypes();
174
175 // Represents what type of block a left brace opens.
176 enum LBraceState {
177 BS_Unknown,
178 BS_Block,
179 BS_BracedInit
180 };
Daniel Jasperbac016b2012-12-03 18:12:45 +0000181
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000182 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
183 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
184 // and use that everywhere in the Parser.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000185 OwningPtr<UnwrappedLine> Line;
Manuel Klimek86721d22013-01-22 16:31:55 +0000186
187 // Comments are sorted into unwrapped lines by whether they are in the same
188 // line as the previous token, or not. If not, they belong to the next token.
189 // Since the next token might already be in a new unwrapped line, we need to
190 // store the comments belonging to that token.
191 SmallVector<FormatToken, 1> CommentsBeforeNextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000192 FormatToken FormatTok;
Manuel Klimek526ed112013-01-09 15:25:02 +0000193 bool MustBreakBeforeNextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000194
Manuel Klimekba287dc2013-01-18 18:24:28 +0000195 // The parsed lines. Only added to through \c CurrentLines.
Manuel Klimek525fe162013-01-18 14:04:34 +0000196 std::vector<UnwrappedLine> Lines;
197
198 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
199 // Thus, we need to keep a list of preprocessor directives to be reported
200 // after an unwarpped line that has been started was finished.
201 std::vector<UnwrappedLine> PreprocessorDirectives;
202
203 // New unwrapped lines are added via CurrentLines.
204 // Usually points to \c &Lines. While parsing a preprocessor directive when
205 // there is an unfinished previous unwrapped line, will point to
206 // \c &PreprocessorDirectives.
207 std::vector<UnwrappedLine> *CurrentLines;
208
Manuel Klimek70b03f42013-01-23 09:32:48 +0000209 // We store for each line whether it must be a declaration depending on
210 // whether we are in a compound statement or not.
211 std::vector<bool> DeclarationScopeStack;
212
Manuel Klimek67d080d2013-04-12 14:13:36 +0000213 // Will be true if we encounter an error that leads to possibily incorrect
214 // indentation levels.
215 bool StructuralError;
216
Alexander Kornienko15757312012-12-06 18:03:27 +0000217 const FormatStyle &Style;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000218 FormatTokenSource *Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000219 UnwrappedLineConsumer &Callback;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000220
Manuel Klimek80829bd2013-05-23 09:41:43 +0000221 // FIXME: This is a temporary measure until we have reworked the ownership
222 // of the format tokens. The goal is to have the actual tokens created and
223 // owned outside of and handed into the UnwrappedLineParser.
224 SmallVector<FormatToken, 16> AllTokens;
225
226 // FIXME: Currently we cannot store attributes with tokens, as we treat
227 // them as read-only; thus, we now store the brace state indexed by the
228 // position of the token in the stream (see \c AllTokens).
229 SmallVector<LBraceState, 16> LBraces;
230
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000231 friend class ScopedLineState;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000232};
233
Daniel Jaspercd162382013-01-07 13:26:07 +0000234} // end namespace format
235} // end namespace clang
Daniel Jasperbac016b2012-12-03 18:12:45 +0000236
Daniel Jaspercd162382013-01-07 13:26:07 +0000237#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H