//===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file implements a token annotator, i.e. creates
/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
///
//===----------------------------------------------------------------------===//
#include "TokenAnnotator.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
namespace clang {
namespace format {
static bool isUnaryOperator(const AnnotatedToken &Tok) {
switch (Tok.FormatTok.Tok.getKind()) {
case tok::plus:
case tok::plusplus:
case tok::minus:
case tok::minusminus:
case tok::exclaim:
case tok::tilde:
case tok::kw_sizeof:
case tok::kw_alignof:
return true;
default:
return false;
}
}
static bool isBinaryOperator(const AnnotatedToken &Tok) {
// Comma is a binary operator, but does not behave as such wrt. formatting.
return getPrecedence(Tok) > prec::Comma;
}
// Returns the previous token ignoring comments.
static AnnotatedToken *getPreviousToken(AnnotatedToken &Tok) {
AnnotatedToken *PrevToken = Tok.Parent;
while (PrevToken != NULL && PrevToken->is(tok::comment))
PrevToken = PrevToken->Parent;
return PrevToken;
}
static const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) {
return getPreviousToken(const_cast<AnnotatedToken &>(Tok));
}
static bool isTrailingComment(AnnotatedToken *Tok) {
return Tok != NULL && Tok->is(tok::comment) &&
(Tok->Children.empty() ||
Tok->Children[0].FormatTok.NewlinesBefore > 0);
}
// Returns the next token ignoring comments.
static const AnnotatedToken *getNextToken(const AnnotatedToken &Tok) {
if (Tok.Children.empty())
return NULL;
const AnnotatedToken *NextToken = &Tok.Children[0];
while (NextToken->is(tok::comment)) {
if (NextToken->Children.empty())
return NULL;
NextToken = &NextToken->Children[0];
}
return NextToken;
}
/// \brief A parser that gathers additional information about tokens.
///
/// The \c TokenAnnotator tries to matches parenthesis and square brakets and
/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
/// into template parameter lists.
class AnnotatingParser {
public:
AnnotatingParser(SourceManager &SourceMgr, Lexer &Lex, AnnotatedLine &Line,
IdentifierInfo &Ident_in)
: SourceMgr(SourceMgr), Lex(Lex), Line(Line), CurrentToken(&Line.First),
KeywordVirtualFound(false), Ident_in(Ident_in) {
Contexts.push_back(Context(1, /*IsExpression=*/ false));
Contexts.back().LookForFunctionName = Line.MustBeDeclaration;
}
private:
bool parseAngle() {
if (CurrentToken == NULL)
return false;
ScopedContextCreator ContextCreator(*this, 10);
AnnotatedToken *Left = CurrentToken->Parent;
Contexts.back().IsExpression = false;
while (CurrentToken != NULL) {
if (CurrentToken->is(tok::greater)) {
Left->MatchingParen = CurrentToken;
CurrentToken->MatchingParen = Left;
CurrentToken->Type = TT_TemplateCloser;
next();
return true;
}
if (CurrentToken->is(tok::r_paren) || CurrentToken