Skip to content

Commit 8b52971

Browse files
committed
Small fixes to unary operator recognition and handling of include
directives. llvm-svn: 169261
1 parent e9bf349 commit 8b52971

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

clang/lib/Format/Format.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "clang/Basic/SourceManager.h"
2222
#include "clang/Lex/Lexer.h"
2323

24+
#include <string>
25+
2426
namespace clang {
2527
namespace format {
2628

@@ -486,13 +488,11 @@ class TokenAnnotator {
486488
Annotation.SpaceRequiredBefore = false;
487489
} else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) {
488490
Annotation.SpaceRequiredBefore =
489-
Line.Tokens[i - 1].Tok.isNot(tok::l_paren);
491+
Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&
492+
Line.Tokens[i - 1].Tok.isNot(tok::l_square);
490493
} else if (Line.Tokens[i - 1].Tok.is(tok::greater) &&
491494
Line.Tokens[i].Tok.is(tok::greater)) {
492-
if (Annotation.Type == TokenAnnotation::TT_TemplateOpener &&
493-
Annotations[i - 1].Type == TokenAnnotation::TT_TemplateOpener)
494-
Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater;
495-
else if (Annotation.Type == TokenAnnotation::TT_TemplateCloser &&
495+
if (Annotation.Type == TokenAnnotation::TT_TemplateCloser &&
496496
Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser)
497497
Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater;
498498
else
@@ -505,6 +505,9 @@ class TokenAnnotator {
505505
Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser &&
506506
Line.Tokens[i].Tok.is(tok::l_paren)) {
507507
Annotation.SpaceRequiredBefore = false;
508+
} else if (Line.Tokens[i].Tok.is(tok::less) &&
509+
Line.Tokens[0].Tok.is(tok::hash)) {
510+
Annotation.SpaceRequiredBefore = true;
508511
} else {
509512
Annotation.SpaceRequiredBefore =
510513
spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok);
@@ -533,7 +536,7 @@ class TokenAnnotator {
533536

534537
if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp))
535538
Annotation.Type = determineStarAmpUsage(i);
536-
else if (Tok.Tok.is(tok::minus) && Line.Tokens[i - 1].Tok.is(tok::equal))
539+
else if (isUnaryOperator(i))
537540
Annotation.Type = TokenAnnotation::TT_UnaryOperator;
538541
else if (isBinaryOperator(Line.Tokens[i]))
539542
Annotation.Type = TokenAnnotation::TT_BinaryOperator;
@@ -548,6 +551,17 @@ class TokenAnnotator {
548551
}
549552
}
550553

554+
bool isUnaryOperator(unsigned Index) {
555+
const Token &Tok = Line.Tokens[Index].Tok;
556+
if (Tok.isNot(tok::minus) && Tok.isNot(tok::plus))
557+
return false;
558+
const Token &PreviousTok = Line.Tokens[Index - 1].Tok;
559+
if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) ||
560+
PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square))
561+
return true;
562+
return Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator;
563+
}
564+
551565
bool isBinaryOperator(const FormatToken &Tok) {
552566
switch (Tok.Tok.getKind()) {
553567
case tok::equal:

clang/unittests/Format/FormatTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
315315

316316
TEST_F(FormatTest, UndestandsUnaryOperators) {
317317
verifyFormat("int a = -2;");
318+
verifyFormat("f(-1, -2, -3);");
319+
verifyFormat("a[-1] = 5;");
320+
verifyFormat("int a = 5 + -2;");
318321
}
319322

320323
TEST_F(FormatTest, UndestandsOverloadedOperators) {
@@ -333,6 +336,13 @@ TEST_F(FormatTest, UnderstandsUsesOfStar) {
333336
// verifyFormat("int a = b * *c;");
334337
}
335338

339+
TEST_F(FormatTest, HandlesIncludeDirectives) {
340+
EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
341+
EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
342+
EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
343+
EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
344+
}
345+
336346
//TEST_F(FormatTest, IncorrectDerivedClass) {
337347
// verifyFormat("public B {\n"
338348
// "};");

0 commit comments

Comments
 (0)