blob: 1326d500e256bf0f54728931401b544b6dc9c14d [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 {
Alexander Kornienko3048aea2013-01-10 15:05:09 +000026
27class DiagnosticsEngine;
28
Daniel Jasperbac016b2012-12-03 18:12:45 +000029namespace format {
30
31/// \brief A wrapper around a \c Token storing information about the
32/// whitespace characters preceeding it.
33struct FormatToken {
Manuel Klimeka080a182013-01-02 16:30:12 +000034 FormatToken()
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000035 : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
Daniel Jasper1eee6c42013-03-04 13:43:19 +000036 LastNewlineOffset(0), TokenLength(0), IsFirst(false),
37 MustBreakBefore(false) {}
Daniel Jasperbac016b2012-12-03 18:12:45 +000038
39 /// \brief The \c Token.
40 Token Tok;
41
42 /// \brief The number of newlines immediately before the \c Token.
43 ///
44 /// This can be used to determine what the user wrote in the original code
45 /// and thereby e.g. leave an empty line between two function definitions.
46 unsigned NewlinesBefore;
47
Manuel Klimeka080a182013-01-02 16:30:12 +000048 /// \brief Whether there is at least one unescaped newline before the \c
49 /// Token.
50 bool HasUnescapedNewline;
51
Daniel Jasperbac016b2012-12-03 18:12:45 +000052 /// \brief The location of the start of the whitespace immediately preceeding
53 /// the \c Token.
54 ///
55 /// Used together with \c WhiteSpaceLength to create a \c Replacement.
56 SourceLocation WhiteSpaceStart;
57
58 /// \brief The length in characters of the whitespace immediately preceeding
59 /// the \c Token.
60 unsigned WhiteSpaceLength;
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000061
Daniel Jasper1eee6c42013-03-04 13:43:19 +000062 /// \brief The offset just past the last '\n' in this token's leading
63 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
64 unsigned LastNewlineOffset;
65
Manuel Klimek95419382013-01-07 07:56:50 +000066 /// \brief The length of the non-whitespace parts of the token. This is
67 /// necessary because we need to handle escaped newlines that are stored
68 /// with the token.
69 unsigned TokenLength;
70
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000071 /// \brief Indicates that this is the first token.
72 bool IsFirst;
Daniel Jasper26f7e782013-01-08 14:56:18 +000073
Manuel Klimek526ed112013-01-09 15:25:02 +000074 /// \brief Whether there must be a line break before this token.
75 ///
76 /// This happens for example when a preprocessor directive ended directly
77 /// before the token.
78 bool MustBreakBefore;
Daniel Jasperbac016b2012-12-03 18:12:45 +000079};
80
81/// \brief An unwrapped line is a sequence of \c Token, that we would like to
82/// put on a single line if there was no column limit.
83///
84/// This is used as a main interface between the \c UnwrappedLineParser and the
85/// \c UnwrappedLineFormatter. The key property is that changing the formatting
86/// within an unwrapped line does not affect any other unwrapped lines.
87struct UnwrappedLine {
Manuel Klimek70b03f42013-01-23 09:32:48 +000088 UnwrappedLine() : Level(0), InPPDirective(false), MustBeDeclaration(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000089 }
90
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +000091 // FIXME: Don't use std::list here.
Daniel Jaspercbb6c412013-01-16 09:10:19 +000092 /// \brief The \c Tokens comprising this \c UnwrappedLine.
93 std::list<FormatToken> Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +000094
95 /// \brief The indent level of the \c UnwrappedLine.
96 unsigned Level;
Manuel Klimeka080a182013-01-02 16:30:12 +000097
98 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
99 bool InPPDirective;
Manuel Klimek70b03f42013-01-23 09:32:48 +0000100
101 bool MustBeDeclaration;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000102};
103
104class UnwrappedLineConsumer {
105public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +0000106 virtual ~UnwrappedLineConsumer() {
107 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +0000108 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000109};
110
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000111class FormatTokenSource {
112public:
Matt Beaumont-Gay422daa12012-12-07 22:49:27 +0000113 virtual ~FormatTokenSource() {
114 }
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000115 virtual FormatToken getNextToken() = 0;
116};
117
Daniel Jasperbac016b2012-12-03 18:12:45 +0000118class UnwrappedLineParser {
119public:
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000120 UnwrappedLineParser(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
121 FormatTokenSource &Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +0000122 UnwrappedLineConsumer &Callback);
123
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000124 /// Returns true in case of a structural error.
125 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000126
127private:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000128 void parseFile();
129 void parseLevel(bool HasOpeningBrace);
130 void parseBlock(bool MustBeDeclaration, unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000131 void parsePPDirective();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000132 void parsePPDefine();
133 void parsePPUnknown();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000134 void parseStructuralElement();
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000135 void parseBracedList();
Manuel Klimekc44ee892013-01-21 10:07:49 +0000136 void parseReturn();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000137 void parseParens();
138 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000139 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000140 void parseDoWhile();
141 void parseLabel();
142 void parseCaseLabel();
143 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000144 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000145 void parseAccessSpecifier();
146 void parseEnum();
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000147 void parseRecord();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000148 void parseObjCProtocolList();
149 void parseObjCUntilAtEnd();
Nico Weber50767d82013-01-09 23:25:37 +0000150 void parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000151 void parseObjCProtocol();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000152 void addUnwrappedLine();
153 bool eof() const;
154 void nextToken();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000155 void readToken();
Manuel Klimek86721d22013-01-22 16:31:55 +0000156 void flushComments(bool NewlineBeforeNext);
157 void pushToken(const FormatToken &Tok);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000158
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000159 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
160 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
161 // and use that everywhere in the Parser.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000162 OwningPtr<UnwrappedLine> Line;
Manuel Klimek86721d22013-01-22 16:31:55 +0000163
164 // Comments are sorted into unwrapped lines by whether they are in the same
165 // line as the previous token, or not. If not, they belong to the next token.
166 // Since the next token might already be in a new unwrapped line, we need to
167 // store the comments belonging to that token.
168 SmallVector<FormatToken, 1> CommentsBeforeNextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000169 FormatToken FormatTok;
Manuel Klimek526ed112013-01-09 15:25:02 +0000170 bool MustBreakBeforeNextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000171
Manuel Klimekba287dc2013-01-18 18:24:28 +0000172 // The parsed lines. Only added to through \c CurrentLines.
Manuel Klimek525fe162013-01-18 14:04:34 +0000173 std::vector<UnwrappedLine> Lines;
174
175 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
176 // Thus, we need to keep a list of preprocessor directives to be reported
177 // after an unwarpped line that has been started was finished.
178 std::vector<UnwrappedLine> PreprocessorDirectives;
179
180 // New unwrapped lines are added via CurrentLines.
181 // Usually points to \c &Lines. While parsing a preprocessor directive when
182 // there is an unfinished previous unwrapped line, will point to
183 // \c &PreprocessorDirectives.
184 std::vector<UnwrappedLine> *CurrentLines;
185
Manuel Klimek70b03f42013-01-23 09:32:48 +0000186 // We store for each line whether it must be a declaration depending on
187 // whether we are in a compound statement or not.
188 std::vector<bool> DeclarationScopeStack;
189
Manuel Klimek67d080d2013-04-12 14:13:36 +0000190 // Will be true if we encounter an error that leads to possibily incorrect
191 // indentation levels.
192 bool StructuralError;
193
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000194 clang::DiagnosticsEngine &Diag;
Alexander Kornienko15757312012-12-06 18:03:27 +0000195 const FormatStyle &Style;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000196 FormatTokenSource *Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000197 UnwrappedLineConsumer &Callback;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000198
199 friend class ScopedLineState;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000200};
201
Daniel Jaspercd162382013-01-07 13:26:07 +0000202} // end namespace format
203} // end namespace clang
Daniel Jasperbac016b2012-12-03 18:12:45 +0000204
Daniel Jaspercd162382013-01-07 13:26:07 +0000205#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H