2013-04-25 16:56:26 +08:00
|
|
|
//===--- WhitespaceManager.h - Format C++ code ------------------*- C++ -*-===//
|
2013-04-15 22:28:00 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief WhitespaceManager class manages whitespace around tokens and their
|
|
|
|
/// replacements.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H
|
|
|
|
#define LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H
|
|
|
|
|
|
|
|
#include "TokenAnnotator.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
/// \brief Manages the whitespaces around tokens and their replacements.
|
|
|
|
///
|
|
|
|
/// This includes special handling for certain constructs, e.g. the alignment of
|
|
|
|
/// trailing line comments.
|
2013-05-22 20:51:29 +08:00
|
|
|
///
|
|
|
|
/// To guarantee correctness of alignment operations, the \c WhitespaceManager
|
|
|
|
/// must be informed about every token in the source file; for each token, there
|
|
|
|
/// must be exactly one call to either \c replaceWhitespace or
|
|
|
|
/// \c addUntouchableToken.
|
|
|
|
///
|
|
|
|
/// There may be multiple calls to \c breakToken for a given token.
|
2013-04-15 22:28:00 +08:00
|
|
|
class WhitespaceManager {
|
|
|
|
public:
|
2013-09-11 20:25:57 +08:00
|
|
|
WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style,
|
|
|
|
bool UseCRLF)
|
|
|
|
: SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-10-12 05:25:45 +08:00
|
|
|
/// \brief Prepares the \c WhitespaceManager for another run.
|
|
|
|
void reset();
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
/// \brief Replaces the whitespace in front of \p Tok. Only call once for
|
|
|
|
/// each \c AnnotatedToken.
|
2013-10-12 05:25:45 +08:00
|
|
|
void replaceWhitespace(FormatToken &Tok, unsigned Newlines,
|
2013-09-28 00:14:22 +08:00
|
|
|
unsigned IndentLevel, unsigned Spaces,
|
|
|
|
unsigned StartOfTokenColumn,
|
2013-05-22 20:51:29 +08:00
|
|
|
bool InPPDirective = false);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
/// \brief Adds information about an unchangeable token's whitespace.
|
2013-04-15 22:28:00 +08:00
|
|
|
///
|
2013-05-22 20:51:29 +08:00
|
|
|
/// Needs to be called for every token for which \c replaceWhitespace
|
|
|
|
/// was not called.
|
|
|
|
void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-06-12 00:01:49 +08:00
|
|
|
/// \brief Inserts or replaces whitespace in the middle of a token.
|
2013-04-15 22:28:00 +08:00
|
|
|
///
|
2013-06-12 00:01:49 +08:00
|
|
|
/// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix
|
|
|
|
/// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars
|
|
|
|
/// characters.
|
2013-04-15 22:28:00 +08:00
|
|
|
///
|
2013-06-12 00:01:49 +08:00
|
|
|
/// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is
|
|
|
|
/// used to align backslashes correctly.
|
|
|
|
void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset,
|
|
|
|
unsigned ReplaceChars,
|
|
|
|
StringRef PreviousPostfix,
|
|
|
|
StringRef CurrentPrefix, bool InPPDirective,
|
2013-09-28 00:14:22 +08:00
|
|
|
unsigned Newlines, unsigned IndentLevel,
|
|
|
|
unsigned Spaces);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
|
|
|
/// \brief Returns all the \c Replacements created during formatting.
|
|
|
|
const tooling::Replacements &generateReplacements();
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
private:
|
|
|
|
/// \brief Represents a change before a token, a break inside a token,
|
|
|
|
/// or the layout of an unchanged token (or whitespace within).
|
|
|
|
struct Change {
|
|
|
|
/// \brief Functor to sort changes in original source order.
|
|
|
|
class IsBeforeInFile {
|
2013-07-08 22:34:09 +08:00
|
|
|
public:
|
2013-05-22 20:51:29 +08:00
|
|
|
IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
|
|
|
|
bool operator()(const Change &C1, const Change &C2) const;
|
|
|
|
|
2013-07-08 22:34:09 +08:00
|
|
|
private:
|
2013-05-22 20:51:29 +08:00
|
|
|
const SourceManager &SourceMgr;
|
|
|
|
};
|
|
|
|
|
|
|
|
Change() {}
|
|
|
|
|
|
|
|
/// \brief Creates a \c Change.
|
|
|
|
///
|
|
|
|
/// The generated \c Change will replace the characters at
|
|
|
|
/// \p OriginalWhitespaceRange with a concatenation of
|
|
|
|
/// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces
|
|
|
|
/// and \p CurrentLinePrefix.
|
|
|
|
///
|
|
|
|
/// \p StartOfTokenColumn and \p InPPDirective will be used to lay out
|
|
|
|
/// trailing comments and escaped newlines.
|
|
|
|
Change(bool CreateReplacement, const SourceRange &OriginalWhitespaceRange,
|
2013-09-28 00:14:22 +08:00
|
|
|
unsigned IndentLevel, unsigned Spaces, unsigned StartOfTokenColumn,
|
2013-05-22 20:51:29 +08:00
|
|
|
unsigned NewlinesBefore, StringRef PreviousLinePostfix,
|
|
|
|
StringRef CurrentLinePrefix, tok::TokenKind Kind,
|
|
|
|
bool ContinuesPPDirective);
|
|
|
|
|
|
|
|
bool CreateReplacement;
|
|
|
|
// Changes might be in the middle of a token, so we cannot just keep the
|
|
|
|
// FormatToken around to query its information.
|
|
|
|
SourceRange OriginalWhitespaceRange;
|
|
|
|
unsigned StartOfTokenColumn;
|
|
|
|
unsigned NewlinesBefore;
|
|
|
|
std::string PreviousLinePostfix;
|
|
|
|
std::string CurrentLinePrefix;
|
|
|
|
// The kind of the token whose whitespace this change replaces, or in which
|
|
|
|
// this change inserts whitespace.
|
|
|
|
// FIXME: Currently this is not set correctly for breaks inside comments, as
|
|
|
|
// the \c BreakableToken is still doing its own alignment.
|
|
|
|
tok::TokenKind Kind;
|
|
|
|
bool ContinuesPPDirective;
|
|
|
|
|
2013-09-28 00:14:22 +08:00
|
|
|
// The number of nested blocks the token is in. This is used to add tabs
|
|
|
|
// only for the indentation, and not for alignment, when
|
|
|
|
// UseTab = US_ForIndentation.
|
|
|
|
unsigned IndentLevel;
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
// The number of spaces in front of the token or broken part of the token.
|
|
|
|
// This will be adapted when aligning tokens.
|
|
|
|
unsigned Spaces;
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
// \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
|
|
|
|
// \c EscapedNewlineColumn will be calculated in
|
|
|
|
// \c calculateLineBreakInformation.
|
|
|
|
bool IsTrailingComment;
|
|
|
|
unsigned TokenLength;
|
|
|
|
unsigned PreviousEndOfTokenColumn;
|
|
|
|
unsigned EscapedNewlineColumn;
|
|
|
|
};
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
/// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens
|
|
|
|
/// or token parts in a line and \c PreviousEndOfTokenColumn and
|
|
|
|
/// \c EscapedNewlineColumn for the first tokens or token parts in a line.
|
|
|
|
void calculateLineBreakInformation();
|
2013-04-25 16:56:26 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
/// \brief Align trailing comments over all \c Changes.
|
|
|
|
void alignTrailingComments();
|
2013-05-13 17:22:11 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
/// \brief Align trailing comments from change \p Start to change \p End at
|
|
|
|
/// the specified \p Column.
|
|
|
|
void alignTrailingComments(unsigned Start, unsigned End, unsigned Column);
|
|
|
|
|
|
|
|
/// \brief Align escaped newlines over all \c Changes.
|
|
|
|
void alignEscapedNewlines();
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
/// \brief Align escaped newlines from change \p Start to change \p End at
|
|
|
|
/// the specified \p Column.
|
|
|
|
void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
/// \brief Fill \c Replaces with the replacements for all effective changes.
|
|
|
|
void generateChanges();
|
|
|
|
|
|
|
|
/// \brief Stores \p Text as the replacement for the whitespace in \p Range.
|
|
|
|
void storeReplacement(const SourceRange &Range, StringRef Text);
|
2013-09-11 20:25:57 +08:00
|
|
|
void appendNewlineText(std::string &Text, unsigned Newlines);
|
|
|
|
void appendNewlineText(std::string &Text, unsigned Newlines,
|
|
|
|
unsigned PreviousEndOfTokenColumn,
|
|
|
|
unsigned EscapedNewlineColumn);
|
2013-09-28 00:14:22 +08:00
|
|
|
void appendIndentText(std::string &Text, unsigned IndentLevel,
|
|
|
|
unsigned Spaces, unsigned WhitespaceStartColumn);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
SmallVector<Change, 16> Changes;
|
2013-04-15 22:28:00 +08:00
|
|
|
SourceManager &SourceMgr;
|
|
|
|
tooling::Replacements Replaces;
|
|
|
|
const FormatStyle &Style;
|
2013-09-11 20:25:57 +08:00
|
|
|
bool UseCRLF;
|
2013-04-15 22:28:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H
|