blob: fb1812852aee2b2805526183ce00eacd9e04f84a [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///
14/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,
15/// where it can be used to format real code.
16///
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
20#define LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H
21
Daniel Jasperbac016b2012-12-03 18:12:45 +000022#include "clang/Basic/IdentifierTable.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Basic/SourceManager.h"
Alexander Kornienko15757312012-12-06 18:03:27 +000024#include "clang/Format/Format.h"
Daniel Jasperbac016b2012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
26
Daniel Jaspercbb6c412013-01-16 09:10:19 +000027#include <list>
Daniel Jasper26f7e782013-01-08 14:56:18 +000028
Daniel Jasperbac016b2012-12-03 18:12:45 +000029namespace clang {
Alexander Kornienko3048aea2013-01-10 15:05:09 +000030
31class DiagnosticsEngine;
32
Daniel Jasperbac016b2012-12-03 18:12:45 +000033namespace format {
34
35/// \brief A wrapper around a \c Token storing information about the
36/// whitespace characters preceeding it.
37struct FormatToken {
Manuel Klimeka080a182013-01-02 16:30:12 +000038 FormatToken()
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000039 : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
Manuel Klimek526ed112013-01-09 15:25:02 +000040 TokenLength(0), IsFirst(false), MustBreakBefore(false) {
Daniel Jasperbac016b2012-12-03 18:12:45 +000041 }
42
43 /// \brief The \c Token.
44 Token Tok;
45
46 /// \brief The number of newlines immediately before the \c Token.
47 ///
48 /// This can be used to determine what the user wrote in the original code
49 /// and thereby e.g. leave an empty line between two function definitions.
50 unsigned NewlinesBefore;
51
Manuel Klimeka080a182013-01-02 16:30:12 +000052 /// \brief Whether there is at least one unescaped newline before the \c
53 /// Token.
54 bool HasUnescapedNewline;
55
Daniel Jasperbac016b2012-12-03 18:12:45 +000056 /// \brief The location of the start of the whitespace immediately preceeding
57 /// the \c Token.
58 ///
59 /// Used together with \c WhiteSpaceLength to create a \c Replacement.
60 SourceLocation WhiteSpaceStart;
61
62 /// \brief The length in characters of the whitespace immediately preceeding
63 /// the \c Token.
64 unsigned WhiteSpaceLength;
Manuel Klimekf6fd00b2013-01-05 22:56:06 +000065
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 Klimeka080a182013-01-02 16:30:12 +000088 UnwrappedLine() : Level(0), InPPDirective(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;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000100};
101
102class UnwrappedLineConsumer {
103public:
Daniel Jasperaccb0b02012-12-04 21:05:31 +0000104 virtual ~UnwrappedLineConsumer() {
105 }
Alexander Kornienko720ffb62012-12-05 13:56:52 +0000106 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000107};
108
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000109class FormatTokenSource {
110public:
Matt Beaumont-Gay422daa12012-12-07 22:49:27 +0000111 virtual ~FormatTokenSource() {
112 }
Alexander Kornienko469a21b2012-12-07 16:15:44 +0000113 virtual FormatToken getNextToken() = 0;
114};
115
Daniel Jasperbac016b2012-12-03 18:12:45 +0000116class UnwrappedLineParser {
117public:
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000118 UnwrappedLineParser(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
119 FormatTokenSource &Tokens,
Daniel Jasperbac016b2012-12-03 18:12:45 +0000120 UnwrappedLineConsumer &Callback);
121
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000122 /// Returns true in case of a structural error.
123 bool parse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000124
125private:
Manuel Klimekd4397b92013-01-04 23:34:14 +0000126 bool parseFile();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000127 bool parseLevel(bool HasOpeningBrace);
Alexander Kornienko15757312012-12-06 18:03:27 +0000128 bool parseBlock(unsigned AddLevels = 1);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000129 void parsePPDirective();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000130 void parsePPDefine();
131 void parsePPUnknown();
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000132 void parseComments();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000133 void parseStructuralElement();
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000134 void parseBracedList();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000135 void parseParens();
136 void parseIfThenElse();
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000137 void parseForOrWhileLoop();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000138 void parseDoWhile();
139 void parseLabel();
140 void parseCaseLabel();
141 void parseSwitch();
Alexander Kornienko15757312012-12-06 18:03:27 +0000142 void parseNamespace();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000143 void parseAccessSpecifier();
144 void parseEnum();
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000145 void parseRecord();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000146 void parseObjCProtocolList();
147 void parseObjCUntilAtEnd();
Nico Weber50767d82013-01-09 23:25:37 +0000148 void parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000149 void parseObjCProtocol();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000150 void addUnwrappedLine();
151 bool eof() const;
152 void nextToken();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000153 void readToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000154
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000155 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
156 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
157 // and use that everywhere in the Parser.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000158 OwningPtr<UnwrappedLine> Line;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000159 FormatToken FormatTok;
Manuel Klimek526ed112013-01-09 15:25:02 +0000160 bool MustBreakBeforeNextToken;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000161
Manuel Klimek525fe162013-01-18 14:04:34 +0000162 // The parsed lines. This is a pointer so we can switch it out to parse an
163 // unrelated set of unwrapped lines and put them into place later.
164 std::vector<UnwrappedLine> Lines;
165
166 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
167 // Thus, we need to keep a list of preprocessor directives to be reported
168 // after an unwarpped line that has been started was finished.
169 std::vector<UnwrappedLine> PreprocessorDirectives;
170
171 // New unwrapped lines are added via CurrentLines.
172 // Usually points to \c &Lines. While parsing a preprocessor directive when
173 // there is an unfinished previous unwrapped line, will point to
174 // \c &PreprocessorDirectives.
175 std::vector<UnwrappedLine> *CurrentLines;
176
Alexander Kornienko3048aea2013-01-10 15:05:09 +0000177 clang::DiagnosticsEngine &Diag;
Alexander Kornienko15757312012-12-06 18:03:27 +0000178 const FormatStyle &Style;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000179 FormatTokenSource *Tokens;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000180 UnwrappedLineConsumer &Callback;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000181
182 friend class ScopedLineState;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000183};
184
Daniel Jaspercd162382013-01-07 13:26:07 +0000185} // end namespace format
186} // end namespace clang
Daniel Jasperbac016b2012-12-03 18:12:45 +0000187
Daniel Jaspercd162382013-01-07 13:26:07 +0000188#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H