if (FormatTok->Tok.is(tok::l_paren))
parseParens();
// handle [[likely]] / [[unlikely]]
- if (FormatTok->is(tok::l_square))
+ if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute())
parseSquare();
bool NeedsUnwrappedLine = false;
if (FormatTok->Tok.is(tok::l_brace)) {
if (FormatTok->Tok.is(tok::kw_else)) {
nextToken();
// handle [[likely]] / [[unlikely]]
- if (FormatTok->is(tok::l_square))
+ if (FormatTok->Tok.is(tok::l_square) && tryToParseSimpleAttribute())
parseSquare();
if (FormatTok->Tok.is(tok::l_brace)) {
CompoundStatementIndenter Indenter(this, Style, Line->Level);
// "} n, m;" will end up in one unwrapped line.
}
+namespace {
+// A class used to set and restore the Token position when peeking
+// ahead in the token source.
+class ScopedTokenPosition {
+ unsigned StoredPosition;
+ FormatTokenSource *Tokens;
+
+public:
+ ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
+ assert(Tokens && "Tokens expected to not be null");
+ StoredPosition = Tokens->getPosition();
+ }
+
+ ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
+};
+} // namespace
+
+// Look to see if we have [[ by looking ahead, if
+// its not then rewind to the original position.
+bool UnwrappedLineParser::tryToParseSimpleAttribute() {
+ ScopedTokenPosition AutoPosition(Tokens);
+ FormatToken *Tok = Tokens->getNextToken();
+ // We already read the first [ check for the second.
+ if (Tok && !Tok->is(tok::l_square)) {
+ return false;
+ }
+ // Double check that the attribute is just something
+ // fairly simple.
+ while (Tok) {
+ if (Tok->is(tok::r_square)) {
+ break;
+ }
+ Tok = Tokens->getNextToken();
+ }
+ Tok = Tokens->getNextToken();
+ if (Tok && !Tok->is(tok::r_square)) {
+ return false;
+ }
+ Tok = Tokens->getNextToken();
+ if (Tok && Tok->is(tok::semi)) {
+ return false;
+ }
+ return true;
+}
+
void UnwrappedLineParser::parseJavaEnumBody() {
// Determine whether the enum is simple, i.e. does not have a semicolon or
// constants with class bodies. Simple enums can be formatted like braced
" }]");
}
+TEST_F(FormatTestObjC, IfNotUnlikely) {
+ Style = getGoogleStyle(FormatStyle::LK_ObjC);
+
+ verifyFormat("if (argc < 5) [obj func:arg];");
+ verifyFormat("if (argc < 5) [[obj1 method1:arg1] method2:arg2];");
+ verifyFormat("if (argc < 5) [[foo bar] baz:i[0]];");
+ verifyFormat("if (argc < 5) [[foo bar] baz:i[0]][1];");
+
+ verifyFormat("if (argc < 5)\n"
+ " [obj func:arg];\n"
+ "else\n"
+ " [obj func:arg2];");
+
+ verifyFormat("if (argc < 5) [[unlikely]]\n"
+ " [obj func:arg];\n"
+ "else [[likely]]\n"
+ " [obj func:arg2];");
+}
+
} // end namespace
} // end namespace format
} // end namespace clang