Commit Graph

650 Commits

Author SHA1 Message Date
Daniel Jasper bf4755bb28 Improve formatting of trailing annotations.
Before:
bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((
    unused));

After:
bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    __attribute__((unused));

llvm-svn: 177034
2013-03-14 09:50:46 +00:00
Daniel Jasper 8a68b95d9e Fix incorrect cast identification.
Before: int a = sizeof(int *)+ b;"
After:  int a = sizeof(int *) + b;
llvm-svn: 176957
2013-03-13 17:13:53 +00:00
Daniel Jasper 0f8ed9e4e1 Fix comment indentation before labels.
Before:
switch (x) {
  // if 1, do f()
case 1:
  f();
}

After:
switch (x) {
// if 1, do f()
case 1:
  f();
}

llvm-svn: 176953
2013-03-13 15:53:12 +00:00
Daniel Jasper 8f6ae19129 Fix formatting issue with builder-type calls.
Before:
->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() - aaaaaaaaa()->aaaaaa()
                                                             ->aaaaa());

After:
a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() -
                         aaaaaaaaa()->aaaaaa()->aaaaa());

llvm-svn: 176952
2013-03-13 15:37:48 +00:00
Daniel Jasper 97b894849e Fix formatting of new arrays of pointers.
Before:
A = new SomeType * [Length];
A = new SomeType *[Length]();

After:
A = new SomeType *[Length];
A = new SomeType *[Length]();

Small formatting cleanups with clang-format.

llvm-svn: 176936
2013-03-13 07:49:51 +00:00
Alexander Kornienko a5151271e6 Implemented formatting of rvalue references
Summary:
Handle "&&" usage as rvalue reference, added tests and fixed incorrect
tests that interfere with this feature.

http://llvm.org/bugs/show_bug.cgi?id=15051

Reviewers: djasper

Reviewed By: djasper

CC: cfe-commits, klimek

Differential Revision: http://llvm-reviews.chandlerc.com/D531

llvm-svn: 176874
2013-03-12 16:28:18 +00:00
Daniel Jasper 181046566c Remove bad space after "default".
Before:
switch (x) {
default : {}
}

After:
switch (x) {
default: {}
}

llvm-svn: 176861
2013-03-12 12:26:55 +00:00
Manuel Klimek 5085d9b275 Fixes breaking of string literals.
1. We now ignore all non-default string literals, including raw
literals.
2. We do not break inside escape sequences any more.

FIXME: We still break in trigraphs.
llvm-svn: 176710
2013-03-08 18:59:48 +00:00
Daniel Jasper f71cf3bd3e Remove unncessary whitespace when triggered on empty line.
With the cursor located at "I", clang-format would not do anything to:

int a;

   I

  int b;

With this patch, it reduces the number of empty lines as necessary, and
removes unnecessary whitespace. It does not change/reformat "int a;" or
"int b;".

llvm-svn: 176650
2013-03-07 20:50:00 +00:00
Edwin Vane 2a760d02f7 Adding lvalue and rvalue reference type matchers
Updated docs and tests.
    
Reviewers: klimek, gribozavr

llvm-svn: 176630
2013-03-07 15:44:40 +00:00
Edwin Vane 0a4836ed0e New ASTMatchers and enhancement to hasOverloadedOperatorName
Added two new narrowing matchers:
* hasMethod: aplies a matcher to a CXXRecordDecl's methods until a match is made
  or there are no more methods.
* hasCanonicalType: applies a matcher to a QualType's canonicalType.

Enhanced hasOverloadedOperatorName to work on CXXMethodDecl as well as
CXXOperatorCallExpr.

Updated tests and docs.

Reviewers: klimek, gribozavr
llvm-svn: 176556
2013-03-06 17:02:57 +00:00
Dmitri Gribenko bcf7f4d3bb Comment parsing: refactor handling of command markers in AST
* Use the term 'command marker', because the semantics of 'backslash' and 'at'
  commands are the same.  (Talking about 'at commands' makes them look like a
  special entity.)

* Sink the flag down into bitfields, reducing the size of AST nodes.

* Change the flag into an enum for clarity.  Boolean function parameters are
  not very clear.

* Add unittests for new tok::at_command tokens.

llvm-svn: 176461
2013-03-04 23:06:15 +00:00
Manuel Klimek abf6e03bc1 Make sure to not split string literals at the first character.
llvm-svn: 176447
2013-03-04 20:03:38 +00:00
Edwin Vane 6972f6d3c7 hasQualifer() matcher should return false if there's no qualifier
Instead of passing NULL on to sub-matcher, just return false. Updated tests and
regenerated docs.

Author: Tareq A Siraj <tareq.a.siraj@intel.com>
llvm-svn: 176441
2013-03-04 17:51:00 +00:00
Daniel Jasper 973c9420e1 Format a line if a range in its leading whitespace was selected.
With [] marking the selected range, clang-format invoked on

    [  ]   int a;

Would so far not reformat anything. With this patch, it formats a
line if its leading whitespace is touched.

llvm-svn: 176435
2013-03-04 13:43:19 +00:00
Peter Collingbourne fe7a348614 CommandLineArgumentParser: handle single quotes.
Differential Revision: http://llvm-reviews.chandlerc.com/D482

llvm-svn: 176404
2013-03-02 06:00:16 +00:00
Fariborz Jahanian e400cb70d4 Some refactoring in my patch on document
command source fidelity. // rdar://13066276

llvm-svn: 176401
2013-03-02 02:39:57 +00:00
Daniel Jasper a79064a88f Remove whitespace at end of file.
This fixes the rest of llvm.org/PR15062.

llvm-svn: 176361
2013-03-01 18:11:39 +00:00
Daniel Jasper 8eb371b3cc Correctly format arrays of pointers and function types.
Before:
void f(Type(*parameter)[10]) {}
int(*func)(void *);

After:
void f(Type (*parameter)[10]) {}
int (*func)(void *);

llvm-svn: 176356
2013-03-01 17:13:29 +00:00
Daniel Jasper f9a84b5f57 Normal indent for last element of builder-type call.
In builder type call, we indent to the laster function calls.
However, for the last element of such a call, we don't need to do
so, as that normally just wastes space and does not increase
readability.

Before:
aaaaaa->aaaaaa->aaaaaa( // break
                  aaaaaa);
aaaaaaaaaaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaa
    ->aaaaaaaaaaaaaaaaaaaaaaaaaa(
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

After:
aaaaaa->aaaaaa->aaaaaa( // break
    aaaaaa);
aaaaaaaaaaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

llvm-svn: 176352
2013-03-01 16:48:32 +00:00
Daniel Jasper 3324cbefcd Remove trailing whitespace of line comments.
This fixed llvm.org/PR15378.

llvm-svn: 176351
2013-03-01 16:45:59 +00:00
Manuel Klimek e317d1b1e8 Implements breaking string literals at slashes.
We now break at a slash if we do not find a space to break on.

Also fixes a bug where we would go over the limit when breaking the
second line.

llvm-svn: 176350
2013-03-01 13:29:19 +00:00
Manuel Klimek b176cff89d Implement fallback split point for string literals.
If we don't find a natural split point (currently space) in a string
literal protruding over the line, we just split at the last possible
point.

llvm-svn: 176349
2013-03-01 13:14:08 +00:00
Daniel Jasper a400cab43a Reduce penalty for splitting after "{" in static initializers.
This fixes llvm.org/PR15379.

Before:
const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = { 0x00, 0x00, 0x00, 0x00, 0x00,
                                            0x00,                  // comment
                                            0x00, 0x00, 0x00, 0x00, 0x00,
                                            0x00,                  // comment
                                            0x00, 0x00, 0x00, 0x00 // comment
};

After:
const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment
  0x00, 0x00, 0x00, 0x00              // comment
};

llvm-svn: 176262
2013-02-28 15:04:12 +00:00
Daniel Jasper 9a23be7451 Dont break between (( in __attribute__((.
Before:
void aaaaaaaaaaaaaaaaaa() __attribute__(
    (aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaa,
     aaaaaaaaaaaaaaaaaaaaaaaaa));

After:
void aaaaaaaaaaaaaaaaaa()
    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaa,
                   aaaaaaaaaaaaaaaaaaaaaaaaa));

llvm-svn: 176260
2013-02-28 14:44:25 +00:00
Daniel Jasper ff6c3a9525 No spaces around pointers to members.
Before: (a ->* f)()
After:  (a->*f)()
llvm-svn: 176252
2013-02-28 13:40:17 +00:00
Manuel Klimek d4be4084b4 First step towards adding a parent map to the ASTContext.
This does not yet implement the LimitNode approach discussed.

The impact of this is an O(n) in the number of nodes in the AST
reduction of complexity for certain kinds of matchers (as otherwise the
parent map gets recreated for every new MatchFinder).

See FIXMEs in the comments for the direction of future work.

llvm-svn: 176251
2013-02-28 13:21:39 +00:00
Daniel Jasper c22f5b4abb Improve formatting of #defines.
Two improvements:
1) Always leave at least one space before "\". Otherwise is can look bad
   and there is a risk of unwillingly joining to characters to a different
   token.
2) Use the full column limit for single-line #defines.
   Fixes llvm.org/PR15148

llvm-svn: 176245
2013-02-28 11:05:57 +00:00
Daniel Jasper 57d4a5821d Fix bug when formatting "A<A<A>>".
Before:
A<A<A>> ReadKansas(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,
                     int aaaaaaaaaaaaaaaaaaaaaaa);
Before:
A<A<A>> ReadKansas(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,
                   int aaaaaaaaaaaaaaaaaaaaaaa);

llvm-svn: 176244
2013-02-28 10:06:05 +00:00
Daniel Jasper ead41b6b8f Fix incorrect recognition of bin-packing.
Before (in Google style):
Constructor()
    : aaaaa(aaaaaa), aaaaa(aaaaaa), aaaaa(aaaaaa), aaaaa(aaaaaa), aaaaa(
          aaaaaa) {}

After:
Constructor()
    : aaaaa(aaaaaa),
      aaaaa(aaaaaa),
      aaaaa(aaaaaa),
      aaaaa(aaaaaa),
      aaaaa(aaaaaa) {}

llvm-svn: 176242
2013-02-28 09:39:12 +00:00
Daniel Jasper d8ffcfa9b8 Fix spacing after binary operator as macro parameter.
Before: COMPARE(a, == , b);
After:  COMPARE(a, ==, b);
llvm-svn: 176241
2013-02-28 09:21:10 +00:00
Manuel Klimek 59b0af6381 Make the negative test of recordType depend on a specific record.
Otherwise it'll break if there's a record type in the AST by default.

llvm-svn: 176181
2013-02-27 11:56:58 +00:00
Daniel Jasper 8e55927153 Fix formatting of multiplications in array subscripts.
Before:
a[a* a] = 1;

After:
a[a * a] = 1;

llvm-svn: 176180
2013-02-27 11:43:50 +00:00
Daniel Jasper 2cf17bfcc1 Enable bin-packing in Google style.
After some discussions, it seems that this is the better path in
the long run. Does not change Chromium style, as there, bin packing
is forbidden by the style guide.

Also fix two minor bugs wrt. formatting:
1. If a call parameter is a function call itself and is split before
   the "." or "->", split before the next parameter.
2. If a call parameter is string literal that has to be split onto
   two lines, split before the next parameter.

llvm-svn: 176177
2013-02-27 09:47:53 +00:00
Daniel Jasper 70bc87420a Fix bad line break decision.
Before:
if (Intervals[i].getRange().getFirst() < Intervals[i - 1]
                                             .getRange().getLast()) {}

After:
if (Intervals[i].getRange().getFirst() <
    Intervals[i - 1].getRange().getLast()) {}

llvm-svn: 176092
2013-02-26 13:59:14 +00:00
Daniel Jasper 16b3562f00 In range-based for-loops, prefer splitting after ":".
Before:
for (const aaaaaaaaaaaaaaaaaaaaa &
         aaaaaaaaa : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}

After:
for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :
     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}

llvm-svn: 176087
2013-02-26 13:18:08 +00:00
Daniel Jasper fb5e241183 Only keep empty lines in unwrapped lines if they preceed a line comment.
Empty lines followed by line comments are often used to highlight the
comment. Empty lines somewhere else are usually left over from manual or
automatic formatting and should probably be removed.

Before (clang-format would keep):
S s = {
  a,

  b
};

After:
S s = { a, b };

llvm-svn: 176086
2013-02-26 13:10:34 +00:00
Daniel Jasper 5497fce41c Only break string literals as a last resort.
We might want to move towards doing this if the formatting can be
significantly improved, but we need to carefully evaluate the different
situations first.

Before (the string literal was split by clang-format here):
aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaa("aaa aaaaa aaa aaa aaaaa aaa "
                                                  "aaaaa aaa aaa aaaaaa"));

After:
aaaaaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaa,
    aaaaaa("aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa"));

llvm-svn: 176084
2013-02-26 12:52:34 +00:00
Edwin Vane b6eae14c15 Various additions to ASTMatcher library:
New type matchers:
* recordType
* elaboratedType

New narrowing matchers:
* hasQualifier
* namesType
* hasDeclContext

Added tests and updated LibASTMatchersReference.

Reviewers: klimek
llvm-svn: 176047
2013-02-25 20:43:32 +00:00
Daniel Jasper 62e681744c Re-add hack that caused regression.
This fixes llvm.org/PR15350.

Before:
Constructor(int Parameter = 0)
    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),
                aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}

After:
Constructor(int Parameter = 0)
    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),
      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}

I think the correct solution is to put the VariablePos into
ParenState, not LineState. Added FIXME.

llvm-svn: 176027
2013-02-25 15:59:54 +00:00
Edwin Vane f901b71921 Adding hasDeclaration overload for TemplateSpecializationType
TemplateSpecializationType doesn't quite have getDecl(). Need to go
through TemplateName to get a TemplateDecl.

Added test cases for the hasDeclaration() overload for
TemplateSpecializationType. Also introduced the type matcher
templateSpecializationType() used by the new hasDeclaration() test case.

Updated LibASTMatchersReference.

Reviewers: klimek
llvm-svn: 176025
2013-02-25 14:49:29 +00:00
Edwin Vane ed93645739 Support in hasDeclaration for types with getDecl()
Re-introducing r175532. The has_getDecl metafunction didn't compile with
Visual Studio. This revision uses approaches has_getDecl from a
different angle that isn't a problem for Visual Studio.

Added dedicated tests for the metafunction.

Reviewers: klimek
llvm-svn: 176024
2013-02-25 14:32:42 +00:00
Edwin Vane d0dfa07b44 Reverting r176019. Corrupt patch.
llvm-svn: 176021
2013-02-25 14:09:28 +00:00
Edwin Vane 8342598d1b Support in hasDeclaration for types with getDecl()
Re-introducing r175532. The has_getDecl metafunction didn't compile with Visual
Studio. This revision approaches has_getDecl from a different angle that isn't
a problem for Visual Studio.

Added dedicated tests for the metafunction.

Reviewers: klimek
llvm-svn: 176019
2013-02-25 13:59:06 +00:00
Daniel Jasper 639522ca35 Add matcher for AccessSpecDecls.
Also, add matchers isPrivate(), isProtected() and isPublic(), that
restrict the matching of such AccessSpecDecls and all other Decls.

llvm-svn: 176017
2013-02-25 12:02:08 +00:00
Daniel Jasper 26d1b1db23 Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.

Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
    const std::string &SomeParameter,
    const SomeType<string,
                   SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
    const SomeType<string,
                   SomeOtherTemplateParameter> &AnotherLongParameterName) {}

After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
    const std::string &SomeParameter,
    const SomeType<string, SomeOtherTemplateParameter> &
        ReallyReallyLongParameterName,
    const SomeType<string, SomeOtherTemplateParameter> &
        AnotherLongParameterName) {}

llvm-svn: 175999
2013-02-24 18:54:32 +00:00
Argyrios Kyrtzidis fead64be9b [preprocessor] Use MacroDirective in the preprocessor callbacks to make available the
full information about the macro (e.g if it was imported and where).

llvm-svn: 175978
2013-02-24 00:05:14 +00:00
Daniel Jasper cd8599e8c9 Better formatting of conditional expressions.
In conditional expressions, if the condition is split over multiple
lines, also break before both operands.

This prevents formattings like:

  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==
      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ? b : c;

Which are bad, because they suggestion incorrect operator precedence:

  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==
      (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ? b : c);

This lead to the discovery that the expression parser incorrectly
handled conditional operators and that it could also handle semicolons
(which in turn reduced the amount of special casing for for-loops). As a
side-effect, we can now apply the bin-packing configuration to the
sections of for-loops.

llvm-svn: 175973
2013-02-23 21:01:55 +00:00
Daniel Jasper ba0bda9739 Don't recognize unnamed pointer parameters as casts.
This fixes llvm.org/PR15061.

Before: virtual void f(int *)const;
After:  virtual void f(int *) const;
llvm-svn: 175960
2013-02-23 08:07:18 +00:00
Daniel Jasper af5ba0ed8c Allow splitting between string literals and identifiers.
Also don't break in long include directives as that is not desired.

We can now format:

  #include "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  #define LL_FORMAT "ll"
  printf("aaaaa: %d, bbbbbbbbb: %" LL_FORMAT "d, cccccccc: %" LL_FORMAT
         "d, ddddddddd: %" LL_FORMAT "d\n");

Before, this led to weird results.

llvm-svn: 175959
2013-02-23 07:46:38 +00:00
Benjamin Kramer 9170e914fc Streamify getNameForDiagnostic and remove the string versions of PrintTemplateArgumentList.
llvm-svn: 175894
2013-02-22 15:46:01 +00:00
Dmitri Gribenko acf2e786d2 Comment parsing: add CommentOptions to allow specifying custom comment block commands
Add an ability to specify custom documentation block comment commands via a new
class CommentOptions.  The intention is that this class will hold future
customizations for comment parsing, including defining documentation comments
with specific numbers of parameters, etc.

CommentOptions instance is a member of LangOptions.

CommentOptions is controlled by a new command-line parameter
-fcomment-block-commands=Foo,Bar,Baz.

llvm-svn: 175892
2013-02-22 14:21:27 +00:00
Daniel Jasper 12ef4e59ef Consistently put {} onto the same line for empty functions.
This fixes llvm.org/PR15167.

Before:
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL()
    : AAAAAAAA(10), BBBBBBBBB(10) {
}
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL() : AAAAAAAA(10) {}

After:
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL()
    : AAAAAAAA(10), BBBBBBBBB(10) {}
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL() : AAAAAAAA(10) {}

llvm-svn: 175800
2013-02-21 21:33:55 +00:00
Daniel Jasper 37905f711c Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.

Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.

Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
         .begin(), E = FD->getDeclsInPrototypeScope().end();
     I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
        ccccccccccccccccccccccccccccc), d, bar(e, f));

After:
for (llvm::ArrayRef<NamedDecl *>::iterator
         I = FD->getDeclsInPrototypeScope().begin(),
         E = FD->getDeclsInPrototypeScope().end();
     I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
        ccccccccccccccccccccccccccccc),
    d, bar(e, f));

llvm-svn: 175741
2013-02-21 15:00:29 +00:00
Manuel Klimek 0c91571d33 Fix regression in string literal alignment.
Now correctly indents (again):
a = a + "a"
        "a"
        "a";

llvm-svn: 175630
2013-02-20 15:32:58 +00:00
Manuel Klimek 02f640a3ab Fixes bug in string literal alignment.
We now indent the following correctly:
1. some + "literal" /* comment */
          "literal";
2. breaking string literals after which we have another string literal.

llvm-svn: 175628
2013-02-20 15:25:48 +00:00
Daniel Jasper dc7d5817e2 Don't remove blank lines within unwrapped lines.
If the code author decides to put empty lines anywhere into the code we
should treat them equally, i.e. reduce them to the configured
MaxEmptyLinesToKeep.

With this change, we e.g. keep the newline in:
SomeType ST = {
  // First value
  a,

  // Second value
  b
};

llvm-svn: 175620
2013-02-20 12:56:39 +00:00
Manuel Klimek 1998ea2c7e Implements breaking of string literals if they stick out.
An alternative strategy to calculating the break on demand when hitting
a token that would need to be broken would be to put all possible breaks
inside the token into the optimizer.

Currently only supports breaking at spaces; more break points to come.

llvm-svn: 175613
2013-02-20 10:15:13 +00:00
Daniel Jasper 6db1b7ca16 Add missing clang-format null pointer check..
.. and a test that triggers it in valid albeit questionable code.

llvm-svn: 175554
2013-02-19 20:05:41 +00:00
Daniel Jasper 0047596cb0 Correctly format macro with unfinished template declaration.
We can now format:
  #define A template <typename T>

Before this created a segfault :-/.

llvm-svn: 175533
2013-02-19 17:14:38 +00:00
Edwin Vane 2c197e0939 Support in hasDeclaration for types with getDecl()
Using a new metafunction for detecting the presence of the member
'getDecl' in a type T, added support to hasDeclaration for any such type
T. This allows hasDecl() to be replaced and enables several other
subclasses of clang::Type to use hasDeclaration.

Updated unittests and LibASTMatchersReference.html.

Reviewers: klimek
llvm-svn: 175532
2013-02-19 17:14:34 +00:00
David Blaikie b61d087d40 Clean up last use of dyn_cast on TypeLoc in ASTMatchers
llvm-svn: 175454
2013-02-18 19:04:16 +00:00
Daniel Jasper e53beb2647 Improve indentation of builder type calls.
In builder-type calls, it can be very confusing to just indent
parameters from the start of the line. Instead, indent 4 from the
correct function call.

Before:
aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break
    aaaaaaaaaaaaaa);
aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()->aaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
    ->aaaaaaaaaaaaaaaaa();

After:
aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break
                                        aaaaaaaaaaaaaa);
aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()
    ->aaaaaaaaaaaaaaaa(
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
    ->aaaaaaaaaaaaaaaaa();

llvm-svn: 175444
2013-02-18 13:52:06 +00:00
Daniel Jasper 9ed9ade0ef Improve formatting of builder-type calls.
Before:
aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaa(aaaaaaaaaaaaaaa);

After:
aaaaaaa->aaaaaaa
    ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
    ->aaaaaaaa(aaaaaaaaaaaaaaa);

llvm-svn: 175441
2013-02-18 13:24:21 +00:00
Daniel Jasper 55d7ba6b66 Reformat lines if they were "moved around".
An unwrapped line can get moved around if there is no newline before
it and the previous line was formatted.

Example:

  template<typename T>  // Cursor is on this line when hitting "format"
  T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }

"return .." is the second unwrapped line in this scenario. I does not
touch any reformatted region. Thus, the result of formatting is:

  template <typename T> T *getFETokenInfo() const { return static_cast<T *>(FETokenInfo); }

After second format (and arguably desired end-result):

  template <typename T> T *getFETokenInfo() const {
      return static_cast<T *>(FETokenInfo);
  }

This fixes: llvm.org/PR15060.

llvm-svn: 175440
2013-02-18 13:08:03 +00:00
Daniel Jasper 5065bc4b2c Correctly determine */& usage in more cases.
This fixes llvm.org/PR15248.

Before:
Test::Test(int b) : a(b *b) {}
for (int i = 0; i < a *a; ++i) {}

After:
Test::Test(int b) : a(b * b) {}
for (int i = 0; i < a * a; ++i) {}

llvm-svn: 175439
2013-02-18 12:44:35 +00:00
Daniel Jasper 2ec3ffb86e Always break after multi-line string literals.
Otherwise, other parameters can be quite hidden.
Reformatted unittests/Format/FormatTest.cpp after this.

Before:
someFunction("Always break between multi-line"
             " string literals", and, other, parameters);

After:
someFunction("Always break between multi-line"
             " string literals",
             and, other, parameters);

llvm-svn: 175436
2013-02-18 11:59:17 +00:00
Daniel Jasper 40c36c54ca Prevent line breaks that make stuff hard to read.
Before:
aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

After:
aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)
    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)
    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,
                         aaaaaaaaaaaaaaaaaaa,
                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

llvm-svn: 175432
2013-02-18 11:05:07 +00:00
Daniel Jasper eb50c676ba Recognize < and > as binary expressions in builder-type calls.
The current heuristic assumes that there can't be binary operators in
builder-type calls (excluding assigments). However, it also excluded
< and > in general, which is wrong. Now they are only excluded if they
are template parameters.

Before:
return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa()i
       .aaaaaa() < aaaaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();

After:
return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <
       aaaaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();

llvm-svn: 175291
2013-02-15 20:33:06 +00:00
Daniel Jasper 0af92ebb20 Done break between 'operator' and '<<'.
Before:
ostream &operator
    <<(ostream &out, some::ns::SomeReallyLongType WithSomeReallyLongValue);

After:
ostream &operator<<(ostream &out,
                    some::ns::SomeReallyLongType WithSomeReallyLongValue);

llvm-svn: 175286
2013-02-15 19:24:08 +00:00
Daniel Jasper 23e8e0c7ea Re-enable ConstructorInitializerAllOnOneLineOrOnePerLine option.
This got lost and was untested as the same effect is achieved by
avoiding bin packing, which is active in Google style by default.
However, moving forward, we want more control over the bin packing
option(s) and thus, this flag should work as expected.

llvm-svn: 175277
2013-02-15 16:49:44 +00:00
Daniel Jasper 54a8602aef Prevent only breaking before "?" in conditional expressions.
This is almost always more readable.

Before:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaaaaaaaa;

After:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa
    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;

llvm-svn: 175262
2013-02-15 11:07:25 +00:00
Daniel Jasper e11095aff1 Fix counting of parameters so that r175162 works as expected.
Before:
aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()
                              .aaaaaaaaaaaaaaaaa());

After:
aaaaaaaaaaaaaaaaaaaaaaaaa(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());

Not sure which of the formattings above is better, but we should not pick
one by accident.

llvm-svn: 175165
2013-02-14 15:01:34 +00:00
Daniel Jasper 687af3bb53 Reduce penalty for breaking before ./-> after complex calls.
This gives a clearer separation of the context, e.g. in GMOCK
statements.

Before:
EXPECT_CALL(SomeObject,
            SomeFunction(Parameter)).WillRepeatedly(Return(SomeValue));

After:
EXPECT_CALL(SomeObject, SomeFunction(Parameter))
    .WillRepeatedly(Return(SomeValue));

Minor format cleanups.

llvm-svn: 175162
2013-02-14 14:26:07 +00:00
Daniel Jasper 2457010b1c Remove the trailing whitespace of formatted lines.
So far, clang-format has always assumed the whitespace belonging to the
subsequent token. This has the negative side-effect that when
clang-format formats a line, it does not remove its trailing whitespace,
as it belongs to the next token.

Thus, this patch fixes most of llvm.org/PR15062.

We are not zapping a file's trailing whitespace so far, as this does not
belong to any token we see during formatting. We need to fix this in a
subsequent patch.

llvm-svn: 175152
2013-02-14 09:58:41 +00:00
Daniel Jasper 66e9dee707 Get less confused by trailing comma in Google style.
The formatter can now format:
void aaaaaaaaaaaaaaaaaa(int level,
                        double *min_x,
                        double *max_x,
                        double *min_y,
                        double *max_y,
                        double *min_z,
                        double *max_z, ) {
}

Although this is invalid code, it frequently happens during development and
clang-format should be nicer :-).

llvm-svn: 175151
2013-02-14 09:19:04 +00:00
Daniel Jasper b315ede9c1 Remove accidentally committed empty test.
llvm-svn: 175148
2013-02-14 08:45:14 +00:00
Daniel Jasper eead02b1b5 Align superclasses for multiple inheritence.
This fixes llvm.org/PR15179.

Before:
class ColorChooserMac : public content::ColorChooser,
    public content::WebContentsObserver {
};

After:
class ColorChooserMac : public content::ColorChooser,
                        public content::WebContentsObserver {
};

llvm-svn: 175147
2013-02-14 08:42:54 +00:00
Daniel Jasper b9caeacd02 Allow breaking after the return type in function declarations.
This has so far been disabled for Google style, but should be done
before breaking at nested name specifiers or in template parameters.

Before (in Google style):
template <typename T>
aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaaaaaaaa<
    T>::aaaaaaa() {}

After:
template <typename T>
aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>
aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}

llvm-svn: 175074
2013-02-13 20:33:44 +00:00
Daniel Jasper 525264c369 Fix comment alignment close to the column limit.
Due to an error in one of the expressions, we used to not align comments
although it would have been possible.

llvm-svn: 175068
2013-02-13 19:25:54 +00:00
Nico Weber 71800c46ae Formatter: And more cast tests (these don't pass yet).
llvm-svn: 175031
2013-02-13 04:40:03 +00:00
Nico Weber 4401b2a4e2 Formatter: Add more cast tests.
llvm-svn: 175030
2013-02-13 04:32:57 +00:00
Nico Weber c6fe2165c9 Formatter: Refactor the cast detection code to be a bit more readable.
No functionality change. Also add another cast test.

llvm-svn: 175029
2013-02-13 04:13:13 +00:00
Nico Weber a789239c64 Formatter: Detect ObjC method expressions after casts.
Not all casts are correctly detected yet, but it helps in some cases.

llvm-svn: 175028
2013-02-13 03:48:27 +00:00
Daniel Jasper f52ab8d60c Fix crash for incomplete labels in macros.
Still the formatting can be improved, but at least we don't assert any
more. This happened when trying to format lib/Sema/SemaType.cpp.

llvm-svn: 175003
2013-02-12 20:17:17 +00:00
Daniel Jasper 2204562acd Fix bug in the adjustment to existing lines.
Before (if only the second line was reformatted):
void f() {}
          void g() {}

After:
void f() {}
void g() {}

llvm-svn: 174978
2013-02-12 16:51:23 +00:00
Nico Weber 4444917f5c Formatter: Correctly format stars in `sizeof(int**)` and similar places.
This redoes how '*' and '&' are classified as pointer / reference markers when
followed by ')', '>', or ','.

Previously, determineStarAmpUsage() marked a single '*' and '&' followed by
')', '>', or ',' as pointer or reference marker. Now, all '*'s and '&'s
preceding ')', '>', or ',' are marked as pointer / reference markers. Fixes
PR14884.

Since only the last '*' in 'int ***' was marked as pointer before (the rest
were unary operators, which don't reach spaceRequiredBetween()),
spaceRequiredBetween() now had to be thought about handing multiple '*'s in
sequence.

Before:
  return sizeof(int * *);
  Type **A = static_cast<Type * *>(P);

Now:
  return sizeof(int**);
  Type **A = static_cast<Type **>(P);

While here, also make all methods of AnnotatingParser except parseLine()
private.

Review URL: http://llvm-reviews.chandlerc.com/D384

llvm-svn: 174975
2013-02-12 16:17:07 +00:00
Edwin Vane e9dd3608ea Adding more overloads for allOf matcher
Adding overloads of allOf accepting 4 and 5 arguments.
    
Reviewer: klimek

llvm-svn: 174967
2013-02-12 13:55:40 +00:00
Nico Weber 29f9dea1ab Formatter: Detect ObjC message expressions after 'in' in loop
Before:
  for (id foo in[self getStuffFor : bla]) {
  }

Now:
  for (id foo in [self getStuffFor:bla]) {
  }

"in" is treated as loop keyword if the line starts with "for", and as a
regular identifier else. To check for "in", its IdentifierInfo is handed
through a few layers.

llvm-svn: 174889
2013-02-11 15:32:15 +00:00
Manuel Klimek 2549956ee3 Get rid of manual debug output, now that the test runner supports it.
You can run tests with -debug instead now.

llvm-svn: 174880
2013-02-11 12:37:30 +00:00
Daniel Jasper eef30490fd Fix invalid formatting with spaces before trailing comments.
In google style, trailing comments are separated by two spaces. This
patch fixes the counting of these spaces and prevents clang-format from
creating a line with 81 columns.

llvm-svn: 174879
2013-02-11 12:36:37 +00:00
Manuel Klimek 0c13795f68 Fixes handling of empty lines in macros.
Now correctly formats:
 #define A \
   \
   b;
to
 #define A b;

Added the state whether an unwrapped line is a macro to the debug
output.

llvm-svn: 174878
2013-02-11 12:33:24 +00:00
Daniel Jasper 35d2dc765e Fix formatting of overloaded operator definitions.
Before:
operatorvoid*();
operator vector< A< A>>();

After:
operator void *();
operator vector<A<A> >();

llvm-svn: 174863
2013-02-11 08:01:18 +00:00
Nico Weber 0448b4dd87 Formatter: Add a test for multi-line ObjC dict literals.
As it turns out, this already works reasonably well.

This example from http://clang.llvm.org/docs/ObjectiveCLiterals.html
NSDictionary *dictionary = @{
    @"name" : NSUserName(),
    @"date" : [NSDate date],
    @"processInfo" : [NSProcessInfo processInfo]
};

is formatted like
NSDictionary *dictionary = @{ @"name" : NSUserName(), @"date" : [NSDate date],
                              @"processInfo" : [NSProcessInfo processInfo] };

There's already a FIXME in NestedStaticInitializers about supporting one
initializer per line, which is really all that's missing here too.

llvm-svn: 174842
2013-02-10 20:48:24 +00:00
Nico Weber 33c68e900b Formatter: Add another ObjC literal test.
(From http://clang.llvm.org/docs/ObjectiveCLiterals.html.)

llvm-svn: 174841
2013-02-10 20:39:05 +00:00
Nico Weber 372d8dcf15 Formatter: Initial support for ObjC dictionary literals.
Before:
  @{
  foo:
    bar
  }
  ;

Now:
  @{ foo : bar };

parseBracedList() already does the right thing from an UnwrappedLineParser
perspective, so check for "@{" in all loops that process constructs that can
contain expressions and call parseBracedList() if found.

llvm-svn: 174840
2013-02-10 20:35:35 +00:00
Nico Weber 2a726b6c34 Formatter: Detect ObjC array literals.
Use this to add a space after "@[" and before "]" for now.

Later, I want to use this to format multi-line array literals nicer, too.

llvm-svn: 174822
2013-02-10 02:08:05 +00:00
Nico Weber 06fcec1cf9 Formatter: Add test for default arguments.
llvm-svn: 174816
2013-02-09 18:02:07 +00:00
Jordan Rose a7d03840e6 Excise <cctype> from Clang (except clang-tblgen) in favor of CharInfo.h.
Nearly all of these changes are one-to-one replacements; the few that
aren't have to do with custom identifier validation.

llvm-svn: 174768
2013-02-08 22:30:41 +00:00
Jordan Rose a2100d755a Pull Lexer's CharInfo table out for general use throughout Clang.
Rewriting the same predicates over and over again is bad for code size and
code maintainence. Using the functions in <ctype.h> is generally unsafe
unless they are specified to be locale-independent (i.e. only isdigit and
isxdigit).

The next commit will try to clean up uses of <ctype.h> functions within Clang.

llvm-svn: 174765
2013-02-08 22:30:22 +00:00