blob: 837b5391f67dab85b61af321240ea45310e1668f [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"
26
Daniel Jasper7c85fde2013-01-08 14:56:18 +000027#include <vector>
28
Daniel Jasperf7935112012-12-03 18:12:45 +000029namespace clang {
Alexander Kornienko5b7157a2013-01-10 15:05:09 +000030
31class DiagnosticsEngine;
32
Daniel Jasperf7935112012-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 Klimeka71e5d82013-01-02 16:30:12 +000038 FormatToken()
Manuel Klimek52d0fd82013-01-05 22:56:06 +000039 : NewlinesBefore(0), HasUnescapedNewline(false), WhiteSpaceLength(0),
Manuel Klimek52b15152013-01-09 15:25:02 +000040 TokenLength(0), IsFirst(false), MustBreakBefore(false) {
Daniel Jasperf7935112012-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 Klimeka71e5d82013-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 Jasperf7935112012-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 Klimek52d0fd82013-01-05 22:56:06 +000065
Manuel Klimekef920692013-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 Klimek52d0fd82013-01-05 22:56:06 +000071 /// \brief Indicates that this is the first token.
72 bool IsFirst;
Daniel Jasper7c85fde2013-01-08 14:56:18 +000073
Manuel Klimek52b15152013-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;
79
Daniel Jasper7c85fde2013-01-08 14:56:18 +000080 // FIXME: We currently assume that there is exactly one token in this vector
81 // except for the very last token that does not have any children.
82 /// \brief All tokens that logically follow this token.
83 std::vector<FormatToken> Children;
Daniel Jasperf7935112012-12-03 18:12:45 +000084};
85
86/// \brief An unwrapped line is a sequence of \c Token, that we would like to
87/// put on a single line if there was no column limit.
88///
89/// This is used as a main interface between the \c UnwrappedLineParser and the
90/// \c UnwrappedLineFormatter. The key property is that changing the formatting
91/// within an unwrapped line does not affect any other unwrapped lines.
92struct UnwrappedLine {
Manuel Klimeka71e5d82013-01-02 16:30:12 +000093 UnwrappedLine() : Level(0), InPPDirective(false) {
Daniel Jasperf7935112012-12-03 18:12:45 +000094 }
95
96 /// \brief The \c Token comprising this \c UnwrappedLine.
Daniel Jasper7c85fde2013-01-08 14:56:18 +000097 FormatToken RootToken;
Daniel Jasperf7935112012-12-03 18:12:45 +000098
99 /// \brief The indent level of the \c UnwrappedLine.
100 unsigned Level;
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000101
102 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
103 bool InPPDirective;
Daniel Jasperf7935112012-12-03 18:12:45 +0000104};
105
106class UnwrappedLineConsumer {
107public:
Daniel Jasper61bd3a12012-12-04 21:05:31 +0000108 virtual ~UnwrappedLineConsumer() {
109 }
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +0000110 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
Daniel Jasperf7935112012-12-03 18:12:45 +0000111};
112
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000113class FormatTokenSource {
114public:
Matt Beaumont-Gay05e0ad52012-12-07 22:49:27 +0000115 virtual ~FormatTokenSource() {
116 }
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000117 virtual FormatToken getNextToken() = 0;
118};
119
Daniel Jasperf7935112012-12-03 18:12:45 +0000120class UnwrappedLineParser {
121public:
Alexander Kornienko5b7157a2013-01-10 15:05:09 +0000122 UnwrappedLineParser(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
123 FormatTokenSource &Tokens,
Daniel Jasperf7935112012-12-03 18:12:45 +0000124 UnwrappedLineConsumer &Callback);
125
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000126 /// Returns true in case of a structural error.
127 bool parse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000128
129private:
Manuel Klimek1abf7892013-01-04 23:34:14 +0000130 bool parseFile();
Manuel Klimek1058d982013-01-06 20:07:31 +0000131 bool parseLevel(bool HasOpeningBrace);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000132 bool parseBlock(unsigned AddLevels = 1);
Daniel Jasperf7935112012-12-03 18:12:45 +0000133 void parsePPDirective();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000134 void parsePPDefine();
135 void parsePPUnknown();
Daniel Jaspere25509f2012-12-17 11:29:41 +0000136 void parseComments();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000137 void parseStructuralElement();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000138 void parseBracedList();
Daniel Jasperf7935112012-12-03 18:12:45 +0000139 void parseParens();
140 void parseIfThenElse();
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000141 void parseForOrWhileLoop();
Daniel Jasperf7935112012-12-03 18:12:45 +0000142 void parseDoWhile();
143 void parseLabel();
144 void parseCaseLabel();
145 void parseSwitch();
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000146 void parseNamespace();
Daniel Jasperf7935112012-12-03 18:12:45 +0000147 void parseAccessSpecifier();
148 void parseEnum();
Manuel Klimek28cacc72013-01-07 18:10:23 +0000149 void parseStructOrClass();
Nico Weber8696a8d2013-01-09 21:15:03 +0000150 void parseObjCProtocolList();
151 void parseObjCUntilAtEnd();
Nico Weber2ce0ac52013-01-09 23:25:37 +0000152 void parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000153 void parseObjCProtocol();
Daniel Jasperf7935112012-12-03 18:12:45 +0000154 void addUnwrappedLine();
155 bool eof() const;
156 void nextToken();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000157 void readToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000158
Manuel Klimekef2cfb12013-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.
Manuel Klimek52b15152013-01-09 15:25:02 +0000162 llvm::OwningPtr<UnwrappedLine> Line;
Daniel Jasper7c85fde2013-01-08 14:56:18 +0000163 bool RootTokenInitialized;
164 FormatToken *LastInCurrentLine;
Daniel Jasperf7935112012-12-03 18:12:45 +0000165 FormatToken FormatTok;
Manuel Klimek52b15152013-01-09 15:25:02 +0000166 bool MustBreakBeforeNextToken;
Daniel Jasperf7935112012-12-03 18:12:45 +0000167
Alexander Kornienko5b7157a2013-01-10 15:05:09 +0000168 clang::DiagnosticsEngine &Diag;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000169 const FormatStyle &Style;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000170 FormatTokenSource *Tokens;
Daniel Jasperf7935112012-12-03 18:12:45 +0000171 UnwrappedLineConsumer &Callback;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000172
173 friend class ScopedLineState;
Daniel Jasperf7935112012-12-03 18:12:45 +0000174};
175
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000176} // end namespace format
177} // end namespace clang
Daniel Jasperf7935112012-12-03 18:12:45 +0000178
Daniel Jasper8d1832e2013-01-07 13:26:07 +0000179#endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H