#include "TokenAnnotator.h"
#include "FormatToken.h"
#include "clang/Basic/SourceManager.h"
+#include "clang/Basic/TokenKinds.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/Debug.h"
AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
const AdditionalKeywords &Keywords)
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
- TrailingReturnFound(false), Keywords(Keywords) {
+ Keywords(Keywords) {
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
resetTokenMetadata(CurrentToken);
}
!Current.Previous->is(tok::kw_operator)) {
// not auto operator->() -> xxx;
Current.Type = TT_TrailingReturnArrow;
- TrailingReturnFound = true;
- } else if (Current.is(tok::star) ||
- (Current.isOneOf(tok::amp, tok::ampamp) &&
- (Current.NestingLevel != 0 || !Line.MightBeFunctionDecl ||
- TrailingReturnFound))) {
+ } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Current.Type = determineStarAmpUsage(Current,
Contexts.back().CanBeExpression &&
Contexts.back().IsExpression,
Current.Type = TT_ConditionalExpr;
}
} else if (Current.isBinaryOperator() &&
- !(Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
- Current.isOneOf(tok::amp, tok::ampamp)) &&
(!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
(!Current.is(tok::greater) &&
Style.Language != FormatStyle::LK_TextProto)) {
// colon after this, this is the only place which annotates the identifier
// as a selector.)
Current.Type = TT_SelectorName;
- } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::amp,
- tok::ampamp) &&
+ } else if (Current.isOneOf(tok::identifier, tok::kw_const,
+ tok::kw_noexcept) &&
Current.Previous &&
!Current.Previous->isOneOf(tok::equal, tok::at) &&
- Line.MightBeFunctionDecl && !TrailingReturnFound &&
- Contexts.size() == 1) {
+ Line.MightBeFunctionDecl && Contexts.size() == 1) {
// Line.MightBeFunctionDecl can only be true after the parentheses of a
// function declaration have been found.
Current.Type = TT_TrailingAnnotation;
const FormatToken *NextToken = Tok.getNextNonComment();
if (!NextToken ||
- NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_const) ||
+ NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_const,
+ tok::kw_noexcept) ||
(NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
return TT_PointerOrReference;
AnnotatedLine &Line;
FormatToken *CurrentToken;
bool AutoFound;
- bool TrailingReturnFound;
const AdditionalKeywords &Keywords;
// Set of "<" tokens that do not open a template parameter list. If parseAngle
verifyFormat("struct f {\n"
" template <class T>\n"
- " int &foo(const std::string &str) & noexcept {}\n"
+ " int &foo(const std::string &str) &noexcept {}\n"
"};",
BreakTemplate);
verifyFormat("struct f {\n"
" template <class T>\n"
- " int &foo(const std::string &str) && noexcept {}\n"
+ " int &foo(const std::string &str) &&noexcept {}\n"
"};",
BreakTemplate);
verifyFormat("struct f {\n"
" template <class T>\n"
- " int &foo(const std::string &str) const & noexcept {}\n"
+ " int &foo(const std::string &str) const &noexcept {}\n"
"};",
BreakTemplate);
verifyFormat("struct f {\n"
" template <class T>\n"
- " int &foo(const std::string &str) const & noexcept {}\n"
+ " int &foo(const std::string &str) const &noexcept {}\n"
"};",
BreakTemplate);
verifyFormat("struct f {\n"
" template <class T>\n"
- " auto foo(const std::string &str) && noexcept -> int & {}\n"
+ " auto foo(const std::string &str) &&noexcept -> int & {}\n"
"};",
BreakTemplate);
verifyFormat("struct f {\n"
" template <class T>\n"
- " int& foo(const std::string& str) const & noexcept {}\n"
+ " int& foo(const std::string& str) const& noexcept {}\n"
"};",
AlignLeftBreakTemplate);
verifyFormat("struct f {\n"
" template <class T>\n"
- " int& foo(const std::string& str) const & noexcept {}\n"
+ " int& foo(const std::string& str) const&& noexcept {}\n"
"};",
AlignLeftBreakTemplate);
" auto foo(const std::string& str) && noexcept -> int& {}\n"
"};",
AlignLeftBreakTemplate);
+
+ // The `&` in `Type&` should not be confused with a trailing `&` of
+ // DEPRECATED(reason) member function.
+ verifyFormat("struct f {\n"
+ " template <class T>\n"
+ " DEPRECATED(reason)\n"
+ " Type &foo(arguments) {}\n"
+ "};",
+ BreakTemplate);
+
+ verifyFormat("struct f {\n"
+ " template <class T>\n"
+ " DEPRECATED(reason)\n"
+ " Type& foo(arguments) {}\n"
+ "};",
+ AlignLeftBreakTemplate);
+
+ verifyFormat("void (*foopt)(int) = &func;");
}
TEST_F(FormatTest, UnderstandsNewAndDelete) {