Home | History | Annotate | Download | only in Format
      1 //===--- WhitespaceManager.h - Format C++ code ------------------*- C++ -*-===//
      2 //
      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 WhitespaceManager class manages whitespace around tokens and their
     12 /// replacements.
     13 ///
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H
     17 #define LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H
     18 
     19 #include "TokenAnnotator.h"
     20 #include "clang/Basic/SourceManager.h"
     21 #include "clang/Format/Format.h"
     22 #include <string>
     23 
     24 namespace clang {
     25 namespace format {
     26 
     27 /// \brief Manages the whitespaces around tokens and their replacements.
     28 ///
     29 /// This includes special handling for certain constructs, e.g. the alignment of
     30 /// trailing line comments.
     31 ///
     32 /// To guarantee correctness of alignment operations, the \c WhitespaceManager
     33 /// must be informed about every token in the source file; for each token, there
     34 /// must be exactly one call to either \c replaceWhitespace or
     35 /// \c addUntouchableToken.
     36 ///
     37 /// There may be multiple calls to \c breakToken for a given token.
     38 class WhitespaceManager {
     39 public:
     40   WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style,
     41                     bool UseCRLF)
     42       : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
     43 
     44   /// \brief Prepares the \c WhitespaceManager for another run.
     45   void reset();
     46 
     47   /// \brief Replaces the whitespace in front of \p Tok. Only call once for
     48   /// each \c AnnotatedToken.
     49   void replaceWhitespace(FormatToken &Tok, unsigned Newlines,
     50                          unsigned IndentLevel, unsigned Spaces,
     51                          unsigned StartOfTokenColumn,
     52                          bool InPPDirective = false);
     53 
     54   /// \brief Adds information about an unchangeable token's whitespace.
     55   ///
     56   /// Needs to be called for every token for which \c replaceWhitespace
     57   /// was not called.
     58   void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
     59 
     60   /// \brief Inserts or replaces whitespace in the middle of a token.
     61   ///
     62   /// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix
     63   /// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars
     64   /// characters.
     65   ///
     66   /// Note: \p Spaces can be negative to retain information about initial
     67   /// relative column offset between a line of a block comment and the start of
     68   /// the comment. This negative offset may be compensated by trailing comment
     69   /// alignment here. In all other cases negative \p Spaces will be truncated to
     70   /// 0.
     71   ///
     72   /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is
     73   /// used to align backslashes correctly.
     74   void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset,
     75                                 unsigned ReplaceChars,
     76                                 StringRef PreviousPostfix,
     77                                 StringRef CurrentPrefix, bool InPPDirective,
     78                                 unsigned Newlines, unsigned IndentLevel,
     79                                 int Spaces);
     80 
     81   /// \brief Returns all the \c Replacements created during formatting.
     82   const tooling::Replacements &generateReplacements();
     83 
     84 private:
     85   /// \brief Represents a change before a token, a break inside a token,
     86   /// or the layout of an unchanged token (or whitespace within).
     87   struct Change {
     88     /// \brief Functor to sort changes in original source order.
     89     class IsBeforeInFile {
     90     public:
     91       IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
     92       bool operator()(const Change &C1, const Change &C2) const;
     93 
     94     private:
     95       const SourceManager &SourceMgr;
     96     };
     97 
     98     Change() {}
     99 
    100     /// \brief Creates a \c Change.
    101     ///
    102     /// The generated \c Change will replace the characters at
    103     /// \p OriginalWhitespaceRange with a concatenation of
    104     /// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces
    105     /// and \p CurrentLinePrefix.
    106     ///
    107     /// \p StartOfTokenColumn and \p InPPDirective will be used to lay out
    108     /// trailing comments and escaped newlines.
    109     Change(bool CreateReplacement, const SourceRange &OriginalWhitespaceRange,
    110            unsigned IndentLevel, int Spaces, unsigned StartOfTokenColumn,
    111            unsigned NewlinesBefore, StringRef PreviousLinePostfix,
    112            StringRef CurrentLinePrefix, tok::TokenKind Kind,
    113            bool ContinuesPPDirective);
    114 
    115     bool CreateReplacement;
    116     // Changes might be in the middle of a token, so we cannot just keep the
    117     // FormatToken around to query its information.
    118     SourceRange OriginalWhitespaceRange;
    119     unsigned StartOfTokenColumn;
    120     unsigned NewlinesBefore;
    121     std::string PreviousLinePostfix;
    122     std::string CurrentLinePrefix;
    123     // The kind of the token whose whitespace this change replaces, or in which
    124     // this change inserts whitespace.
    125     // FIXME: Currently this is not set correctly for breaks inside comments, as
    126     // the \c BreakableToken is still doing its own alignment.
    127     tok::TokenKind Kind;
    128     bool ContinuesPPDirective;
    129 
    130     // The number of nested blocks the token is in. This is used to add tabs
    131     // only for the indentation, and not for alignment, when
    132     // UseTab = US_ForIndentation.
    133     unsigned IndentLevel;
    134 
    135     // The number of spaces in front of the token or broken part of the token.
    136     // This will be adapted when aligning tokens.
    137     // Can be negative to retain information about the initial relative offset
    138     // of the lines in a block comment. This is used when aligning trailing
    139     // comments. Uncompensated negative offset is truncated to 0.
    140     int Spaces;
    141 
    142     // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
    143     // \c EscapedNewlineColumn will be calculated in
    144     // \c calculateLineBreakInformation.
    145     bool IsTrailingComment;
    146     unsigned TokenLength;
    147     unsigned PreviousEndOfTokenColumn;
    148     unsigned EscapedNewlineColumn;
    149 
    150     // These fields are used to retain correct relative line indentation in a
    151     // block comment when aligning trailing comments.
    152     //
    153     // If this Change represents a continuation of a block comment,
    154     // \c StartOfBlockComment is pointer to the first Change in the block
    155     // comment. \c IndentationOffset is a relative column offset to this
    156     // change, so that the correct column can be reconstructed at the end of
    157     // the alignment process.
    158     const Change *StartOfBlockComment;
    159     int IndentationOffset;
    160   };
    161 
    162   /// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens
    163   /// or token parts in a line and \c PreviousEndOfTokenColumn and
    164   /// \c EscapedNewlineColumn for the first tokens or token parts in a line.
    165   void calculateLineBreakInformation();
    166 
    167   /// \brief Align trailing comments over all \c Changes.
    168   void alignTrailingComments();
    169 
    170   /// \brief Align trailing comments from change \p Start to change \p End at
    171   /// the specified \p Column.
    172   void alignTrailingComments(unsigned Start, unsigned End, unsigned Column);
    173 
    174   /// \brief Align escaped newlines over all \c Changes.
    175   void alignEscapedNewlines();
    176 
    177   /// \brief Align escaped newlines from change \p Start to change \p End at
    178   /// the specified \p Column.
    179   void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
    180 
    181   /// \brief Fill \c Replaces with the replacements for all effective changes.
    182   void generateChanges();
    183 
    184   /// \brief Stores \p Text as the replacement for the whitespace in \p Range.
    185   void storeReplacement(const SourceRange &Range, StringRef Text);
    186   void appendNewlineText(std::string &Text, unsigned Newlines);
    187   void appendNewlineText(std::string &Text, unsigned Newlines,
    188                          unsigned PreviousEndOfTokenColumn,
    189                          unsigned EscapedNewlineColumn);
    190   void appendIndentText(std::string &Text, unsigned IndentLevel,
    191                         unsigned Spaces, unsigned WhitespaceStartColumn);
    192 
    193   SmallVector<Change, 16> Changes;
    194   SourceManager &SourceMgr;
    195   tooling::Replacements Replaces;
    196   const FormatStyle &Style;
    197   bool UseCRLF;
    198 };
    199 
    200 } // namespace format
    201 } // namespace clang
    202 
    203 #endif // LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H
    204