blob: 227b023a81b569d9775a7bc82891bb007f293efe [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"
Alexander Kornienko3b711552013-06-03 16:45:03 +000021#include "FormatToken.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
Daniel Jasperbac016b2012-12-03 18:12:45 +000027/// \brief An unwrapped line is a sequence of \c Token, that we would like to
28/// put on a single line if there was no column limit.
29///
30/// This is used as a main interface between the \c UnwrappedLineParser and the
31/// \c UnwrappedLineFormatter. The key property is that changing the formatting
32/// within an unwrapped line does not affect any other unwrapped lines.
33struct UnwrappedLine {
Manuel Klimek70b03f42013-01-23 09:32:48 +000034 UnwrappedLine() : Level(0), InPPDirective(false), MustBeDeclaration(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000035 }
36
Daniel Jasper3f8cdbf2013-01-16 10:41:46 +000037 // FIXME: Don't use std::list here.
Daniel Jaspercbb6c412013-01-16 09:10:19 +000038 /// \brief The \c Tokens comprising this \c UnwrappedLine.
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +000039 std::list<FormatToken *> Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +000040
41 /// \brief The indent level of the \c UnwrappedLine.
42 unsigned Level;
Manuel Klimeka080a182013-01-02 16:30:12 +000043
44 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
45 bool InPPDirective;
Manuel Klimek70b03f42013-01-23 09:32:48 +000046
47 bool MustBeDeclaration;
Daniel Jasperbac016b2012-12-03 18:12:45 +000048};
49
50class UnwrappedLineConsumer {
51public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +000052 virtual ~UnwrappedLineConsumer() {
53 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +000054 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +000055};
56
Manuel Klimek96e888b2013-05-28 11:55:06 +000057class FormatTokenSource;
Alexander Kornienko469a21b2012-12-07 16:15:44 +000058
Daniel Jasperbac016b2012-12-03 18:12:45 +000059class UnwrappedLineParser {
60public:
Manuel Klimek96e888b2013-05-28 11:55:06 +000061 UnwrappedLineParser(const FormatStyle &Style, ArrayRef<FormatToken *> Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +000062 UnwrappedLineConsumer &Callback);
63
Alexander Kornienkocff563c2012-12-04 17:27:50 +000064 /// Returns true in case of a structural error.
65 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +000066
67private:
Manuel Klimek67d080d2013-04-12 14:13:36 +000068 void parseFile();
69 void parseLevel(bool HasOpeningBrace);
70 void parseBlock(bool MustBeDeclaration, unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +000071 void parsePPDirective();
Manuel Klimekd4397b92013-01-04 23:34:14 +000072 void parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +000073 void parsePPIf();
74 void parsePPIfdef();
75 void parsePPElIf();
76 void parsePPElse();
77 void parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +000078 void parsePPUnknown();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +000079 void parseStructuralElement();
Manuel Klimek80829bd2013-05-23 09:41:43 +000080 bool tryToParseBracedList();
Manuel Klimekbb42bf12013-01-10 11:52:21 +000081 void parseBracedList();
Manuel Klimekc44ee892013-01-21 10:07:49 +000082 void parseReturn();
Daniel Jasperbac016b2012-12-03 18:12:45 +000083 void parseParens();
84 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +000085 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +000086 void parseDoWhile();
87 void parseLabel();
88 void parseCaseLabel();
89 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +000090 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +000091 void parseAccessSpecifier();
92 void parseEnum();
Manuel Klimek47ea7f62013-01-15 13:38:33 +000093 void parseRecord();
Nico Weber1abe6ea2013-01-09 21:15:03 +000094 void parseObjCProtocolList();
95 void parseObjCUntilAtEnd();
Nico Weber50767d82013-01-09 23:25:37 +000096 void parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +000097 void parseObjCProtocol();
Daniel Jasperbac016b2012-12-03 18:12:45 +000098 void addUnwrappedLine();
99 bool eof() const;
100 void nextToken();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000101 void readToken();
Manuel Klimek86721d22013-01-22 16:31:55 +0000102 void flushComments(bool NewlineBeforeNext);
Manuel Klimek96e888b2013-05-28 11:55:06 +0000103 void pushToken(FormatToken *Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000104 void calculateBraceTypes();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000105 void pushPPConditional();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000106
107 // Represents what type of block a left brace opens.
108 enum LBraceState {
109 BS_Unknown,
110 BS_Block,
111 BS_BracedInit
112 };
Daniel Jasperbac016b2012-12-03 18:12:45 +0000113
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000114 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
115 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
116 // and use that everywhere in the Parser.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000117 OwningPtr<UnwrappedLine> Line;
Manuel Klimek86721d22013-01-22 16:31:55 +0000118
119 // Comments are sorted into unwrapped lines by whether they are in the same
120 // line as the previous token, or not. If not, they belong to the next token.
121 // Since the next token might already be in a new unwrapped line, we need to
122 // store the comments belonging to that token.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000123 SmallVector<FormatToken *, 1> CommentsBeforeNextToken;
124 FormatToken *FormatTok;
Manuel Klimek526ed112013-01-09 15:25:02 +0000125 bool MustBreakBeforeNextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000126
Manuel Klimekba287dc2013-01-18 18:24:28 +0000127 // The parsed lines. Only added to through \c CurrentLines.
Manuel Klimek525fe162013-01-18 14:04:34 +0000128 std::vector<UnwrappedLine> Lines;
129
130 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
131 // Thus, we need to keep a list of preprocessor directives to be reported
132 // after an unwarpped line that has been started was finished.
133 std::vector<UnwrappedLine> PreprocessorDirectives;
134
135 // New unwrapped lines are added via CurrentLines.
136 // Usually points to \c &Lines. While parsing a preprocessor directive when
137 // there is an unfinished previous unwrapped line, will point to
138 // \c &PreprocessorDirectives.
139 std::vector<UnwrappedLine> *CurrentLines;
140
Manuel Klimek70b03f42013-01-23 09:32:48 +0000141 // We store for each line whether it must be a declaration depending on
142 // whether we are in a compound statement or not.
143 std::vector<bool> DeclarationScopeStack;
144
Manuel Klimek67d080d2013-04-12 14:13:36 +0000145 // Will be true if we encounter an error that leads to possibily incorrect
146 // indentation levels.
147 bool StructuralError;
148
Alexander Kornienko15757312012-12-06 18:03:27 +0000149 const FormatStyle &Style;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000150 FormatTokenSource *Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000151 UnwrappedLineConsumer &Callback;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000152
Manuel Klimek80829bd2013-05-23 09:41:43 +0000153 // FIXME: This is a temporary measure until we have reworked the ownership
154 // of the format tokens. The goal is to have the actual tokens created and
155 // owned outside of and handed into the UnwrappedLineParser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000156 ArrayRef<FormatToken *> AllTokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000157
158 // FIXME: Currently we cannot store attributes with tokens, as we treat
159 // them as read-only; thus, we now store the brace state indexed by the
160 // position of the token in the stream (see \c AllTokens).
161 SmallVector<LBraceState, 16> LBraces;
162
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000163 // Represents preprocessor branch type, so we can find matching
164 // #if/#else/#endif directives.
165 enum PPBranchKind {
166 PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
167 PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0
168 };
169
170 // Keeps a stack of currently active preprocessor branching directives.
171 SmallVector<PPBranchKind, 16> PPStack;
172
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000173 friend class ScopedLineState;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000174};
175
Daniel Jaspercd162382013-01-07 13:26:07 +0000176} // end namespace format
177} // end namespace clang
Daniel Jasperbac016b2012-12-03 18:12:45 +0000178
Daniel Jaspercd162382013-01-07 13:26:07 +0000179#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H