encodes the canonical rules for LLVM's style. I noticed this had drifted
quite a bit when cleaning up LLVM, so wanted to clean up Clang as well.
llvm-svn: 198686
Before:
enum ShortEnum {
A,
B,
C
};
After:
enum ShortEnum { A, B, C };
This seems to be the predominant choice in LLVM/Clang as well as in
Google style.
llvm-svn: 198558
type_info has been made an implicitly predeclared type in r198497 and will no
longer appear as a user-declared type so we can remove this old hack.
This reverts commit r158595.
llvm-svn: 198502
important for thread safety attributes, which contain expressions that were
not being visited, and were thus invisible to various tools. There are now
Visit*Attr methods that can be overridden for every attribute.
llvm-svn: 198224
A comment following the "{" of a braced list seems to almost always
refer to the first element of the list and thus should be aligned
to it.
Before (with Cpp11 braced list style):
SomeFunction({ // Comment 1
"first entry",
// Comment 2
"second entry"});
After:
SomeFunction({// Comment 1
"first entry",
// Comment 2
"second entry"});
llvm-svn: 197725
Unexpectedly, it seems that people commonly know what they were doing
when writing a comment.
Also, being more conservative about comment breaking has the advantage
of giving more flexibility. If a linebreak within the comment can
improve formatting, the author can add it (after which clang-format
won't undo it). There is no way to override clang-format's behavior if
it breaks a comment.
llvm-svn: 197698
Before:
aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();
After:
aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();
Probably still not ideal, but should be a step into the right direction.
llvm-svn: 197557
Formatting this:
void f() { // 1 space initial indent.
int i;
#define A \
int i; \
int j;
int k; // Format this line.
}
void f() {
#define A 1 // Format this line.
}
Before:
void f() { // 1 space initial indent.
int i;
#define A \
int i; \
int j;
int k; // Format this line.
}
void f() {
#define A 1 // Format this line.
}
After:
void f() { // 1 space initial indent.
int i;
#define A \
int i; \
int j;
int k; // Format this line.
}
void f() {
#define A 1 // Format this line.
}
llvm-svn: 197494
BreakConstructorInitializersBeforeComma is true.
This option is used in WebKit style, so this also ensures initializer lists are
not put on a single line, as per the WebKit coding guidelines.
Patch by Florian Sowade!
llvm-svn: 197386
Especially try to keep existing line breaks before raw string literals,
as the code author might have aligned content to it.
Thereby, clang-format now keeps things like:
parseStyle(R"(
BasedOnStyle: Google,
ColumnLimit: 100)");
parseStyle(
R"(BasedOnStyle: Google,
ColumnLimit: 100)");
llvm-svn: 197368
__builtin_va_list and friends have been showing up where they shouldn't for way
to long, making unwanted appearences in -ast-print, tooling and source level
visitors and even the hello world tutorial on the clang website.
This commit factors down the implicit typedef and record creation facilities to
ensure they're marked implicit.
Also fixes a unit test that was testing incorrect behaviour, and removes old
hacks in the DeclPrinter that tried to skip implicit declarations manually.
llvm-svn: 197336
Summary:
Added BraceBreakingStyle::BS_GNU. I'm not sure about the correctness of
static initializer formatting, but compound statements should be fine.
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2372
llvm-svn: 197138
Summary:
This still misses a few important features, so there's no mention of
this style in the help message, but a few style rules are implemented.
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2371
llvm-svn: 196928
Summary:
Allow predefined styles to define different options for different
languages so that one can run:
clang-format -style=google file1.cpp file2.js
or use a single .clang-format file with "BasedOnStyle: Google" for both c++ and
JS files.
Added Google style for JavaScript with "BreakBeforeTernaryOperators" set to
false.
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2364
llvm-svn: 196909
Summary:
The rule from the GNU style states:
"We find it easier to read a program when it has spaces before the open-parentheses and after the commas."
http://www.gnu.org/prep/standards/standards.html#index-spaces-before-open_002dparen
This patch makes clang-format adds an option to put spaces before almost all open parentheses, except the cases, where different behavior is dictated by the style rules or language syntax:
* preprocessor:
** function-like macro definitions can't have a space between the macro name and the parenthesis;
** `#if defined(...)` can have a space, but it seems, that it's more frequently used without a space in GCC, for example;
* never add spaces after unary operators;
* adding spaces between two opening parentheses is controlled with the `SpacesInParentheses` option;
* never add spaces between `[` and `(` (there's no option yet).
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2326
llvm-svn: 196901
Summary:
Allow tryFitMultipleLinesInOne join unwrapped lines when
ContinuationIndenter::mustBreak doesn't agree. But don't merge any lines, that
are separate in the input.
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2321
llvm-svn: 196378
Before:
int aaaa; // This line is formatted.
// The comment continues ..
// .. here.
Before:
int aaaa; // This line is formatted.
// The comment continues ..
// .. here.
This fixes llvm.org/PR17914.
llvm-svn: 195954
Previously, clang-format could create quite corrupt formattings if
individual lines of nested blocks (e.g. in "DEBUG({})" or lambdas) were
used. With this patch, it tries to extend the formatted regions to leave
around some reasonable format without always formatting the entire
surrounding statement.
llvm-svn: 195925
Summary:
getStringSplit used to crash, when trying to split a long string
literal containing both printable and unprintable multi-byte UTF-8 characters.
Reviewers: djasper, klimek
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2268
llvm-svn: 195728
No functional changes intended. However, it seems to have found a buggy
behavior in one of the tests. I think this structure is generally
desirable and it will make a planned bugfix significantly easier.
llvm-svn: 195634
Add a severe penalty for not using column layout for braced lists. If
there are solutions with column layout, these are generally preferable
over bin-packed solutions.
Before:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{ aaaaaaa, aaaaaaaaaa, aaaaa,
aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,
aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaa,
aaaaaaa, a };
After:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{
aaaaaaa, aaaaaaaaaa,
aaaaa, aaaaaaaaaaaaaaa,
aaa, aaaaaaaaaa,
a, aaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,
aaaaaaa, a
};
llvm-svn: 195546
Summary:
Make the negative test more specific.
Otherwise it can accidentally match injected code.
Reviewers: aaron.ballman
CC: klimek, cfe-commits, revane
Differential Revision: http://llvm-reviews.chandlerc.com/D2257
llvm-svn: 195512
Summary: Add support for the 'unless' matcher in the dynamic layer.
Reviewers: klimek
CC: cfe-commits, revane, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2247
llvm-svn: 195466
Summary:
Now based on token merging. Now they are not only prevented from being
split, but are actually formatted as comparison operators.
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2240
llvm-svn: 195354
In particular, make breaking after a parameter's ":" more of a last
resort choice as it significantly affects the readability gained by
aligning the parameters.
Before (in Chromium style - which doesn't allow bin-packing):
{
popup_window_.reset([[RenderWidgetPopupWindow alloc]
initWithContentRect:
NSMakeRect(
origin_global.x, origin_global.y, pos.width(), pos.height())
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO]);
}
After:
{
popup_window_.reset([[RenderWidgetPopupWindow alloc]
initWithContentRect:NSMakeRect(origin_global.x,
origin_global.y,
pos.width(),
pos.height())
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO]);
}
llvm-svn: 195301
Summary:
The AllowShortFunctionsOnASingleLine option now controls short function
body placement on a single line independent of the BreakBeforeBraces option.
Updated tests using BreakBeforeBraces other than BS_Attach.
Addresses http://llvm.org/PR17888
Reviewers: klimek, djasper
Reviewed By: klimek
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2230
llvm-svn: 195256
Summary:
Add partial support for the hasDeclaration() matcher in the dynamic layer.
This matcher has some special logic to allow any type that has a getDecl() method. We do not support this right now.
Reviewers: klimek
CC: cfe-commits, revane
Differential Revision: http://llvm-reviews.chandlerc.com/D1889
llvm-svn: 195013
FixedCompilationDatabase (FCD) requires that the arguments it consumes after
'--' must not include positional parameters or the argv[0] of the tool. This
patch relaxes those restrictions.
llvm-svn: 194968
Before, existing code in the form of:
int a; // this is a.
// This is
// b.
int b;
Got turned into:
int a; // this is a.
// This is
// b.
int b;
llvm-svn: 194294
Before:
unsigned ContentSize =
sizeof(int16_t) // DWARF ARange version number
+
sizeof(int32_t) // Offset of CU in the .debug_info section
+
sizeof(int8_t) // Pointer Size (in bytes)
+
sizeof(int8_t); // Segment Size (in bytes)
After:
unsigned ContentSize =
sizeof(int16_t) // DWARF ARange version number
+ sizeof(int32_t) // Offset of CU in the .debug_info section
+ sizeof(int8_t) // Pointer Size (in bytes)
+ sizeof(int8_t); // Segment Size (in bytes)
This fixes llvm.org/PR17687.
llvm-svn: 194276
As a side-effect, constructors definitions will correctly be recognized
and formatted as function declarations. Tests will be added in a
follow-up patch actually using the correct recognition.
llvm-svn: 194209
These allow clients to retrieve persistent AST objects (ASTUnits) which
can be used in an ad-hoc manner after parsing.
To accommodate this change, the code for processing a CompilerInvocation
using a FrontendAction has been factored out to FrontendActionFactory, and
a new base class, ToolAction, has been introduced, allowing the tool to do
arbitrary things with each CompilerInvocation. This change was necessary
because ASTUnit does not use the FrontendAction interface directly.
This change also causes the FileManager in ClangTool to use shared ownership.
This will become necessary because ASTUnit takes shared ownership of
FileManager (ClangTool's FileManager is currently unused by ASTUnit; this
is a FIXME). As shown in the tests, any client of ToolInvocation will
need to be modified to use shared ownership for FileManager.
Differential Revision: http://llvm-reviews.chandlerc.com/D2097
llvm-svn: 194164
These functions can generally be applied to multiple kinds of AST node,
so it makes sense to add them to DynTypedNode.
Differential Revision: http://llvm-reviews.chandlerc.com/D2096
llvm-svn: 194113
The purpose of this function is to allow clients of the dynamic AST matcher
to enumerate each binding.
Differential Revision: http://llvm-reviews.chandlerc.com/D2095
llvm-svn: 194112
Before, clang-format would always format entire nested blocks, which
can be unwanted e.g. for long DEBUG({...}) statements. Also
clang-format would not allow to merge lines in nested blocks (e.g. to
put "if (a) return;" on one line in Google style).
This is the first step of several refactorings mostly focussing on the
additional functionality (by reusing the "format many lines" code to
format the children of a nested block). The next steps are:
* Pull out the line merging into its own class.
* Seperate the formatting of many lines from the formatting of a single
line (and the analysis of the solution space).
llvm-svn: 194090
Same as SpacesInParentheses, this option allows adding a space inside
the '<' and '>' of a template parameter list.
Patch by Christopher Olsen.
This fixes llvm.org/PR17301.
llvm-svn: 193614
Summary: This resubmits r193100, plus a fix for a breakage with MSVC.
Reviewers: klimek, rnk
CC: cfe-commits, revane
Differential Revision: http://llvm-reviews.chandlerc.com/D2005
llvm-svn: 193613
Specifically make clang-format less eager to break after the opening
parenthesis of a function call.
Before:
aaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
After:
aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
Apparently that is preferable. This penalties are adapted
conservatively, we might have to increase them a little bit further.
llvm-svn: 193410
This fixes llvm.org/PR17682.
Without this patch, the following code leads to invalid reads/writes:
DEBUG({
return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
});
#if a
#else
#endif
Because of the #if-#else structure, the code is formatted and annotated
twice and becauce of the nested block, the annotated lines form a
hierarchical structure. This structure was not properly reset between
runs.
llvm-svn: 193352
Specifically, if a braced list has at least one nested braced list,
format it either all on one line or in one column (i.e. one item per
line).
This seems in general to be an improvement as the structure of nested
braced lists can make a tightly packed outer braced list hard to read.
llvm-svn: 193345
This allows using virtual file mappings on the original SourceManager to
map in virtual module.map files. Without this patch, the ModuleMap
search will find a module.map file (as the FileEntry exists in the
FileManager), but will be unable to get the content from the
SourceManager (as ModuleMap previously created its own SourceManager).
Two problems needed to be fixed which this patch exposed:
1. Storing the inferred module map
When writing out a module, the ASTWriter stores the names of the files
in the main source manager; when loading the AST again, the ASTReader
errs out if such a file is found missing, unless it is overridden.
Previously CompilerInstance's compileModule method would store the
inferred module map to a temporary file; the problem with this approach
is that now that the module map is handled by the main source manager,
the ASTWriter stores the name of the temporary module map as source to
the compilation; later, when the module is loaded, the temporary file
has already been deleted, which leads to a compilation error. This patch
changes the inferred module map to instead inject a virtual file into
the source manager. This both saves some disk IO, and works with how the
ASTWriter/ASTReader handle overridden source files.
2. Changing test input in test/Modules/Inputs/*
Now that the module map file is handled by the main source manager, the
VerifyDiagnosticConsumer will not ignore diagnostics created while
parsing the module map file. The module test test/Modules/renamed.m uses
-I test/Modules/Inputs and triggers recursive loading of all module maps
in test/Modules/Inputs, some of which had conflicting names, thus
leading errors while parsing the module maps. Those diagnostics already
occur on trunk, but before this patch they would not break the test, as
they were ignored by the VerifyDiagnosticConsumer. This patch thus
changes the module maps that have been recently introduced which broke
the invariant of compatible modules maps in test/Modules/Inputs.
llvm-svn: 193314