blob: c79c35e1b9d62d4f4601a0f323f581ce373dd4f5 [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
16#ifndef LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
17#define LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
18
Daniel Jasperf7935112012-12-03 18:12:45 +000019#include "clang/Basic/IdentifierTable.h"
Alexander Kornienko578fdd82012-12-06 18:03:27 +000020#include "clang/Format/Format.h"
Alexander Kornienko4b672072013-06-03 16:45:03 +000021#include "FormatToken.h"
Daniel Jasperdaffc0d2013-01-16 09:10:19 +000022#include <list>
Daniel Jasper7c85fde2013-01-08 14:56:18 +000023
Daniel Jasperf7935112012-12-03 18:12:45 +000024namespace clang {
25namespace format {
26
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000027struct UnwrappedLineNode;
28
Daniel Jasperf7935112012-12-03 18:12:45 +000029/// \brief An unwrapped line is a sequence of \c Token, that we would like to
30/// put on a single line if there was no column limit.
31///
32/// This is used as a main interface between the \c UnwrappedLineParser and the
33/// \c UnwrappedLineFormatter. The key property is that changing the formatting
34/// within an unwrapped line does not affect any other unwrapped lines.
35struct UnwrappedLine {
Douglas Gregor7cfed212013-09-05 18:28:53 +000036 UnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +000037
Daniel Jaspera67a8f02013-01-16 10:41:46 +000038 // FIXME: Don't use std::list here.
Daniel Jasperdaffc0d2013-01-16 09:10:19 +000039 /// \brief The \c Tokens comprising this \c UnwrappedLine.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000040 std::list<UnwrappedLineNode> Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +000041
42 /// \brief The indent level of the \c UnwrappedLine.
43 unsigned Level;
Manuel Klimeka71e5d82013-01-02 16:30:12 +000044
45 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
46 bool InPPDirective;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000047
48 bool MustBeDeclaration;
Daniel Jasperf7935112012-12-03 18:12:45 +000049};
50
51class UnwrappedLineConsumer {
52public:
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000053 virtual ~UnwrappedLineConsumer() {}
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +000054 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperf7935112012-12-03 18:12:45 +000055};
56
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000057class FormatTokenSource;
Alexander Kornienkoe3276842012-12-07 16:15:44 +000058
Daniel Jasperf7935112012-12-03 18:12:45 +000059class UnwrappedLineParser {
60public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000061 UnwrappedLineParser(const FormatStyle &Style, ArrayRef<FormatToken *> Tokens,
Daniel Jasperf7935112012-12-03 18:12:45 +000062 UnwrappedLineConsumer &Callback);
63
Alexander Kornienko870f9eb2012-12-04 17:27:50 +000064 /// Returns true in case of a structural error.
65 bool parse();
Daniel Jasperf7935112012-12-03 18:12:45 +000066
67private:
Manuel Klimek1a18c402013-04-12 14:13:36 +000068 void parseFile();
69 void parseLevel(bool HasOpeningBrace);
Daniel Jasper65ee3472013-07-31 23:16:02 +000070 void parseBlock(bool MustBeDeclaration, bool AddLevel = true);
Manuel Klimek516e0542013-09-04 13:25:30 +000071 void parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +000072 void parsePPDirective();
Manuel Klimek1abf7892013-01-04 23:34:14 +000073 void parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +000074 void parsePPIf();
75 void parsePPIfdef();
76 void parsePPElIf();
77 void parsePPElse();
78 void parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +000079 void parsePPUnknown();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +000080 void parseStructuralElement();
Manuel Klimekab419912013-05-23 09:41:43 +000081 bool tryToParseBracedList();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +000082 void parseBracedList();
Manuel Klimek762dd182013-01-21 10:07:49 +000083 void parseReturn();
Daniel Jasperf7935112012-12-03 18:12:45 +000084 void parseParens();
85 void parseIfThenElse();
Alexander Kornienko37d6c942012-12-05 15:06:06 +000086 void parseForOrWhileLoop();
Daniel Jasperf7935112012-12-03 18:12:45 +000087 void parseDoWhile();
88 void parseLabel();
89 void parseCaseLabel();
90 void parseSwitch();
Alexander Kornienko578fdd82012-12-06 18:03:27 +000091 void parseNamespace();
Daniel Jasperf7935112012-12-03 18:12:45 +000092 void parseAccessSpecifier();
93 void parseEnum();
Manuel Klimeke01bab52013-01-15 13:38:33 +000094 void parseRecord();
Nico Weber8696a8d2013-01-09 21:15:03 +000095 void parseObjCProtocolList();
96 void parseObjCUntilAtEnd();
Nico Weber2ce0ac52013-01-09 23:25:37 +000097 void parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +000098 void parseObjCProtocol();
Manuel Klimekffdeb592013-09-03 15:10:01 +000099 void tryToParseLambda();
100 bool tryToParseLambdaIntroducer();
Daniel Jasperf7935112012-12-03 18:12:45 +0000101 void addUnwrappedLine();
102 bool eof() const;
103 void nextToken();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000104 void readToken();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000105 void flushComments(bool NewlineBeforeNext);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000106 void pushToken(FormatToken *Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000107 void calculateBraceTypes();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000108 void pushPPConditional();
Manuel Klimekab419912013-05-23 09:41:43 +0000109
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000110 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
111 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
112 // and use that everywhere in the Parser.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000113 OwningPtr<UnwrappedLine> Line;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000114
115 // Comments are sorted into unwrapped lines by whether they are in the same
116 // line as the previous token, or not. If not, they belong to the next token.
117 // Since the next token might already be in a new unwrapped line, we need to
118 // store the comments belonging to that token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000119 SmallVector<FormatToken *, 1> CommentsBeforeNextToken;
120 FormatToken *FormatTok;
Manuel Klimek52b15152013-01-09 15:25:02 +0000121 bool MustBreakBeforeNextToken;
Daniel Jasperf7935112012-12-03 18:12:45 +0000122
Manuel Klimek05d82b72013-01-18 18:24:28 +0000123 // The parsed lines. Only added to through \c CurrentLines.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000124 SmallVector<UnwrappedLine, 8> Lines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000125
126 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
127 // Thus, we need to keep a list of preprocessor directives to be reported
128 // after an unwarpped line that has been started was finished.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000129 SmallVector<UnwrappedLine, 4> PreprocessorDirectives;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000130
131 // New unwrapped lines are added via CurrentLines.
132 // Usually points to \c &Lines. While parsing a preprocessor directive when
133 // there is an unfinished previous unwrapped line, will point to
134 // \c &PreprocessorDirectives.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000135 SmallVectorImpl<UnwrappedLine> *CurrentLines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000136
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000137 // We store for each line whether it must be a declaration depending on
138 // whether we are in a compound statement or not.
139 std::vector<bool> DeclarationScopeStack;
140
Manuel Klimek1a18c402013-04-12 14:13:36 +0000141 // Will be true if we encounter an error that leads to possibily incorrect
142 // indentation levels.
143 bool StructuralError;
144
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000145 const FormatStyle &Style;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000146 FormatTokenSource *Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +0000147 UnwrappedLineConsumer &Callback;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000148
Manuel Klimekab419912013-05-23 09:41:43 +0000149 // FIXME: This is a temporary measure until we have reworked the ownership
150 // of the format tokens. The goal is to have the actual tokens created and
151 // owned outside of and handed into the UnwrappedLineParser.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000152 ArrayRef<FormatToken *> AllTokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000153
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000154 // Represents preprocessor branch type, so we can find matching
155 // #if/#else/#endif directives.
156 enum PPBranchKind {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000157 PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
158 PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000159 };
160
161 // Keeps a stack of currently active preprocessor branching directives.
162 SmallVector<PPBranchKind, 16> PPStack;
163
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000164 friend class ScopedLineState;
Daniel Jasperf7935112012-12-03 18:12:45 +0000165};
166
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000167struct UnwrappedLineNode {
168 UnwrappedLineNode() : Tok(NULL) {}
169 UnwrappedLineNode(FormatToken *Tok) : Tok(Tok) {}
170
171 FormatToken *Tok;
172 SmallVector<UnwrappedLine, 0> Children;
173};
174
Douglas Gregor7cfed212013-09-05 18:28:53 +0000175inline UnwrappedLine::UnwrappedLine()
176 : Level(0), InPPDirective(false), MustBeDeclaration(false) {}
177
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000178} // end namespace format
179} // end namespace clang
Daniel Jasperf7935112012-12-03 18:12:45 +0000180
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000181#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H