132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===//
232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//
332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//                     The LLVM Compiler Infrastructure
432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//
532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// This file is distributed under the University of Illinois Open Source
632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// License. See LICENSE.TXT for details.
732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//
832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//===----------------------------------------------------------------------===//
932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper///
1032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \file
1132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \brief This file implements a token annotator, i.e. creates
1232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
1332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper///
1432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//===----------------------------------------------------------------------===//
1532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
1632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#ifndef LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
1732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
1832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
1932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#include "UnwrappedLineParser.h"
2032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#include "clang/Format/Format.h"
2132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#include <string>
2232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
2332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jaspernamespace clang {
2432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass SourceManager;
2532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
2632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jaspernamespace format {
2732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
2832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperenum LineType {
2932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LT_Invalid,
3032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LT_Other,
3132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LT_BuilderTypeCall,
3232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LT_PreprocessorDirective,
3332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LT_VirtualFunctionDecl,
3432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
3532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LT_ObjCMethodDecl,
3632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LT_ObjCProperty // An @property line.
3732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper};
3832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
3932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass AnnotatedLine {
4032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperpublic:
4132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  AnnotatedLine(const UnwrappedLine &Line)
4232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      : First(Line.Tokens.front()), Level(Line.Level),
4332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        InPPDirective(Line.InPPDirective),
4453e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper        MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
4553e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper        StartsDefinition(false) {
4632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    assert(!Line.Tokens.empty());
47b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    FormatToken *Current = First;
48dcb3f2aab329c80e97a44312d15299df4dc059beManuel Klimek    for (std::list<FormatToken *>::const_iterator I = ++Line.Tokens.begin(),
49dcb3f2aab329c80e97a44312d15299df4dc059beManuel Klimek                                                  E = Line.Tokens.end();
5032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper         I != E; ++I) {
51b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      Current->Next = *I;
52b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      (*I)->Previous = Current;
53b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      Current = Current->Next;
5432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    }
5532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    Last = Current;
5632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
5732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
58b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  FormatToken *First;
59b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  FormatToken *Last;
6032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
6132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LineType Type;
6232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  unsigned Level;
6332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  bool InPPDirective;
6432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  bool MustBeDeclaration;
653c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper  bool MightBeFunctionDecl;
6653e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper  bool StartsDefinition;
6732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper};
6832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
6932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \brief Determines extra information about the tokens comprising an
7032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \c UnwrappedLine.
7132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass TokenAnnotator {
7232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperpublic:
7300895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko  TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
7400895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko      : Style(Style), Ident_in(Ident_in) {}
7532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
768ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper  void annotate(AnnotatedLine &Line);
778ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper  void calculateFormattingInformation(AnnotatedLine &Line);
7832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
7932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperprivate:
8032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  /// \brief Calculate the penalty for splitting before \c Tok.
81b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
8232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
83b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
84b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek                            const FormatToken &Right);
8532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
86b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
8732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
88b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
8932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
90bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper  void printDebugInfo(const AnnotatedLine &Line);
91bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper
92e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek  void calculateUnbreakableTailLengths(AnnotatedLine &Line);
93e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek
948ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper  const FormatStyle &Style;
95c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber
96c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber  // Contextual keywords:
97c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber  IdentifierInfo &Ident_in;
9832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper};
9932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
10032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // end namespace format
10132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // end namespace clang
10232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
10332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
104