blob: 47148ef66d779f2672697e635334ae6827de6574 [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///
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 Jasperf7935112012-12-03 18:12:45 +000022#include "clang/Basic/IdentifierTable.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Basic/SourceManager.h"
Alexander Kornienko578fdd82012-12-06 18:03:27 +000024#include "clang/Format/Format.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000025#include "clang/Lex/Lexer.h"
Daniel Jasperdaffc0d2013-01-16 09:10:19 +000026#include <list>
Daniel Jasper7c85fde2013-01-08 14:56:18 +000027
Daniel Jasperf7935112012-12-03 18:12:45 +000028namespace clang {
Alexander Kornienko5b7157a2013-01-10 15:05:09 +000029
30class DiagnosticsEngine;
31
Daniel Jasperf7935112012-12-03 18:12:45 +000032namespace format {
33
34/// \brief A wrapper around a \c Token storing information about the
35/// whitespace characters preceeding it.
36struct FormatToken {
Manuel Klimeka71e5d82013-01-02 16:30:12 +000037 FormatToken()
Manuel Klimek52d0fd82013-01-05 22:56:06 +000038 : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
Manuel Klimek52b15152013-01-09 15:25:02 +000039 TokenLength(0), IsFirst(false), MustBreakBefore(false) {
Daniel Jasperf7935112012-12-03 18:12:45 +000040 }
41
42 /// \brief The \c Token.
43 Token Tok;
44
45 /// \brief The number of newlines immediately before the \c Token.
46 ///
47 /// This can be used to determine what the user wrote in the original code
48 /// and thereby e.g. leave an empty line between two function definitions.
49 unsigned NewlinesBefore;
50
Manuel Klimeka71e5d82013-01-02 16:30:12 +000051 /// \brief Whether there is at least one unescaped newline before the \c
52 /// Token.
53 bool HasUnescapedNewline;
54
Daniel Jasperf7935112012-12-03 18:12:45 +000055 /// \brief The location of the start of the whitespace immediately preceeding
56 /// the \c Token.
57 ///
58 /// Used together with \c WhiteSpaceLength to create a \c Replacement.
59 SourceLocation WhiteSpaceStart;
60
61 /// \brief The length in characters of the whitespace immediately preceeding
62 /// the \c Token.
63 unsigned WhiteSpaceLength;
Manuel Klimek52d0fd82013-01-05 22:56:06 +000064
Manuel Klimekef920692013-01-07 07:56:50 +000065 /// \brief The length of the non-whitespace parts of the token. This is
66 /// necessary because we need to handle escaped newlines that are stored
67 /// with the token.
68 unsigned TokenLength;
69
Manuel Klimek52d0fd82013-01-05 22:56:06 +000070 /// \brief Indicates that this is the first token.
71 bool IsFirst;
Daniel Jasper7c85fde2013-01-08 14:56:18 +000072
Manuel Klimek52b15152013-01-09 15:25:02 +000073 /// \brief Whether there must be a line break before this token.
74 ///
75 /// This happens for example when a preprocessor directive ended directly
76 /// before the token.
77 bool MustBreakBefore;
Daniel Jasperf7935112012-12-03 18:12:45 +000078};
79
80/// \brief An unwrapped line is a sequence of \c Token, that we would like to
81/// put on a single line if there was no column limit.
82///
83/// This is used as a main interface between the \c UnwrappedLineParser and the
84/// \c UnwrappedLineFormatter. The key property is that changing the formatting
85/// within an unwrapped line does not affect any other unwrapped lines.
86struct UnwrappedLine {
Manuel Klimeka71e5d82013-01-02 16:30:12 +000087 UnwrappedLine() : Level(0), InPPDirective(false) {
Daniel Jasperf7935112012-12-03 18:12:45 +000088 }
89
Daniel Jaspera67a8f02013-01-16 10:41:46 +000090 // FIXME: Don't use std::list here.
Daniel Jasperdaffc0d2013-01-16 09:10:19 +000091 /// \brief The \c Tokens comprising this \c UnwrappedLine.
92 std::list<FormatToken> Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +000093
94 /// \brief The indent level of the \c UnwrappedLine.
95 unsigned Level;
Manuel Klimeka71e5d82013-01-02 16:30:12 +000096
97 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
98 bool InPPDirective;
Daniel Jasperf7935112012-12-03 18:12:45 +000099};
100
101class UnwrappedLineConsumer {
102public:
Daniel Jasper61bd3a12012-12-04 21:05:31 +0000103 virtual ~UnwrappedLineConsumer() {
104 }
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +0000105 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperf7935112012-12-03 18:12:45 +0000106};
107
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000108class FormatTokenSource {
109public:
Matt Beaumont-Gay05e0ad52012-12-07 22:49:27 +0000110 virtual ~FormatTokenSource() {
111 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000112 virtual FormatToken getNextToken() = 0;
113};
114
Daniel Jasperf7935112012-12-03 18:12:45 +0000115class UnwrappedLineParser {
116public:
Alexander Kornienko5b7157a2013-01-10 15:05:09 +0000117 UnwrappedLineParser(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
118 FormatTokenSource &Tokens,
Daniel Jasperf7935112012-12-03 18:12:45 +0000119 UnwrappedLineConsumer &Callback);
120
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000121 /// Returns true in case of a structural error.
122 bool parse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000123
124private:
Manuel Klimek1abf7892013-01-04 23:34:14 +0000125 bool parseFile();
Manuel Klimek1058d982013-01-06 20:07:31 +0000126 bool parseLevel(bool HasOpeningBrace);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000127 bool parseBlock(unsigned AddLevels = 1);
Daniel Jasperf7935112012-12-03 18:12:45 +0000128 void parsePPDirective();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000129 void parsePPDefine();
130 void parsePPUnknown();
Daniel Jaspere25509f2012-12-17 11:29:41 +0000131 void parseComments();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000132 void parseStructuralElement();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000133 void parseBracedList();
Manuel Klimek762dd182013-01-21 10:07:49 +0000134 void parseReturn();
Daniel Jasperf7935112012-12-03 18:12:45 +0000135 void parseParens();
136 void parseIfThenElse();
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000137 void parseForOrWhileLoop();
Daniel Jasperf7935112012-12-03 18:12:45 +0000138 void parseDoWhile();
139 void parseLabel();
140 void parseCaseLabel();
141 void parseSwitch();
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000142 void parseNamespace();
Daniel Jasperf7935112012-12-03 18:12:45 +0000143 void parseAccessSpecifier();
144 void parseEnum();
Manuel Klimeke01bab52013-01-15 13:38:33 +0000145 void parseRecord();
Nico Weber8696a8d2013-01-09 21:15:03 +0000146 void parseObjCProtocolList();
147 void parseObjCUntilAtEnd();
Nico Weber2ce0ac52013-01-09 23:25:37 +0000148 void parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000149 void parseObjCProtocol();
Daniel Jasperf7935112012-12-03 18:12:45 +0000150 void addUnwrappedLine();
151 bool eof() const;
152 void nextToken();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000153 void readToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000154
Manuel Klimekef2cfb12013-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 Gribenkof8579502013-01-12 19:30:44 +0000158 OwningPtr<UnwrappedLine> Line;
Daniel Jasperf7935112012-12-03 18:12:45 +0000159 FormatToken FormatTok;
Manuel Klimek52b15152013-01-09 15:25:02 +0000160 bool MustBreakBeforeNextToken;
Daniel Jasperf7935112012-12-03 18:12:45 +0000161
Manuel Klimek05d82b72013-01-18 18:24:28 +0000162 // The parsed lines. Only added to through \c CurrentLines.
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000163 std::vector<UnwrappedLine> Lines;
164
165 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
166 // Thus, we need to keep a list of preprocessor directives to be reported
167 // after an unwarpped line that has been started was finished.
168 std::vector<UnwrappedLine> PreprocessorDirectives;
169
170 // New unwrapped lines are added via CurrentLines.
171 // Usually points to \c &Lines. While parsing a preprocessor directive when
172 // there is an unfinished previous unwrapped line, will point to
173 // \c &PreprocessorDirectives.
174 std::vector<UnwrappedLine> *CurrentLines;
175
Alexander Kornienko5b7157a2013-01-10 15:05:09 +0000176 clang::DiagnosticsEngine &Diag;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000177 const FormatStyle &Style;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000178 FormatTokenSource *Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +0000179 UnwrappedLineConsumer &Callback;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000180
181 friend class ScopedLineState;
Daniel Jasperf7935112012-12-03 18:12:45 +0000182};
183
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000184} // end namespace format
185} // end namespace clang
Daniel Jasperf7935112012-12-03 18:12:45 +0000186
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000187#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H