Commit Graph

5154 Commits

Author SHA1 Message Date
Gregory Fong
e57f57841f [clang-format] fix alignment w/o binpacked args
The combination of

- AlignConsecutiveAssignments.Enabled = true
- BinPackArguments = false

would result in the first continuation line of a braced-init-list being
improperly indented (missing a shift) when in a continued function call.
Indentation was also wrong for braced-init-lists continuing a
direct-list-initialization.  Check for opening braced lists in
continuation and ensure that the correct shift occurs.

Fixes https://github.com/llvm/llvm-project/issues/55360

Reviewed By: curdeius

Differential Revision: https://reviews.llvm.org/D125162
2022-05-16 10:25:06 +02:00
Marek Kurdej
e20bc892b6 [clang-format] Fix PointerAlignment: Right not working with tab indentation.
Fixes https://github.com/llvm/llvm-project/issues/55407.

Given configuration:
```
UseTab: Always
PointerAlignment: Right
AlignConsecutiveDeclarations: true
```

Before, the pointer was misaligned in this code:
```
void f() {
	unsigned long long big;
	char	      *ptr; // misaligned
	int		   i;
}
```

That was due to the fact that when handling right-aligned pointers, the Spaces were changed but StartOfTokenColumn was not.

Also, a tab was used not only for indentation but for spacing too when using `UseTab: ForIndentation` config option:
```
void f() {
	unsigned long long big;
	char	      *ptr; // \t after char
	int                i;
}
```

Reviewed By: owenpan

Differential Revision: https://reviews.llvm.org/D125528
2022-05-16 09:42:20 +02:00
owenca
2cdabc0322 [clang-format] Handle "if consteval { ... }" for RemoveBracesLLVM
Differential Revision: https://reviews.llvm.org/D125593
2022-05-15 01:33:44 -07:00
owenca
2601355dc9 [clang-format][NFC] Format unit tests with insert/remove braces
This patch is the result of running clang-format version 753fe33 in
clang/unittests/Format/:
clang-format -style="{InsertBraces: true, RemoveBracesLLVM: true}" -i *.cpp *.h

Differential Revision: https://reviews.llvm.org/D125510
2022-05-13 16:16:20 -07:00
Nico Weber
e0fcdf5496 Revert "In MSVC compatibility mode, friend function declarations behave as function declarations"
This reverts commit ad47114ad8.
See discussion on https://reviews.llvm.org/D124613.
2022-05-13 09:48:01 -04:00
Whisperity
9add949557 [ASTMatchers][clang-tidy][NFC] Hoist forEachTemplateArgument matcher into the core library
Fixes the `FIXME:` related to adding `forEachTemplateArgument` to the
core AST Matchers library.

Reviewed By: aaron.ballman

Differential Revision: http://reviews.llvm.org/D125383
2022-05-13 12:55:48 +02:00
owenca
6cd9633c1d [clang-format] Handle comments below r_brace in RemoveBracesLLVM
If a closing brace is followed by a non-trailing comment, the
newline before the closing brace must also be removed.

Differential Revision: https://reviews.llvm.org/D125451
2022-05-12 16:51:18 -07:00
owenca
b6d8c84f28 [clang-format] Don't remove braces if a 1-statement body would wrap
Reimplement the RemoveBracesLLVM feature which handles a
single-statement block that would get wrapped.

Fixes #53543.

Differential Revision: https://reviews.llvm.org/D125137
2022-05-12 03:53:08 -07:00
Whisperity
06a98328fc [ASTMatchers][NFC] Fix name of matcher in docs and add a missing test 2022-05-11 14:15:53 +02:00
Sergey Semushin
dab5e10ea5 [clang-format] fix nested angle brackets parse inside concept definition
Due to how parseBracedList always stopped on the first closing angle
bracket and was used in parsing angle bracketed expression inside concept
definition, nested brackets inside concepts were parsed incorrectly.

nextToken() call before calling parseBracedList is required because
we were processing opening angle bracket inside parseBracedList second
time leading to incorrect logic after my fix.

Fixes https://github.com/llvm/llvm-project/issues/54943
Fixes https://github.com/llvm/llvm-project/issues/54837

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D123896
2022-05-11 14:02:51 +02:00
Sam McCall
00a3c9f2a4 [Frontend] Flip default of CreateInvocationOptions::ProbePrecompiled to false
This is generally a better default for tools other than the compiler, which
shouldn't assume a PCH file on disk is something they can consume.

Preserve the old behavior in places associated with libclang/c-index-test
(including ASTUnit) as there are tests relying on it and most important
consumers are out-of-tree. It's unclear whether the tests are specifically
trying to test this functionality, and what the downstream implications of
removing it are. Hopefully someone more familiar can clean this up in future.

Differential Revision: https://reviews.llvm.org/D125149
2022-05-10 16:58:13 +02:00
Marek Kurdej
85ec8a9ac1 [clang-format] Correctly handle SpaceBeforeParens for builtins.
That's a partial fix for https://github.com/llvm/llvm-project/issues/55292.

Before, known builtins behaved differently from other identifiers:
```
void f () { return F (__builtin_LINE() + __builtin_FOO ()); }
```
After:
```
void f () { return F (__builtin_LINE () + __builtin_FOO ()); }
```

Reviewed By: owenpan

Differential Revision: https://reviews.llvm.org/D125085
2022-05-09 11:42:41 +02:00
Marek Kurdej
50cd52d935 [clang-format] Fix WhitespaceSensitiveMacros not being honoured when macro closing parenthesis is followed by a newline.
Fixes https://github.com/llvm/llvm-project/issues/54522.

This fixes regression introduced in 5e5efd8a91.

Before the culprit commit, macros in WhitespaceSensitiveMacros were correctly formatted even if their closing parenthesis weren't followed by semicolon (or, to be precise, when they were followed by a newline).
That commit changed the type of the macro token type from TT_UntouchableMacroFunc to TT_FunctionLikeOrFreestandingMacro.

Correct formatting (with `WhitespaceSensitiveMacros = ['FOO']`):
```
FOO(1+2)
FOO(1+2);
```

Regressed formatting:
```
FOO(1 + 2)
FOO(1+2);
```

Reviewed By: HazardyKnusperkeks, owenpan, ksyx

Differential Revision: https://reviews.llvm.org/D123676
2022-05-09 10:59:33 +02:00
Sam McCall
f44552ab38 [Frontend] Fix broken createInvocation test due to bad merge 2022-05-06 17:06:13 +02:00
Sam McCall
499d0b96cb [clang] createInvocationFromCommandLine -> createInvocation, delete former. NFC
(Followup from 40c13720a4)

Differential Revision: https://reviews.llvm.org/D125012
2022-05-06 16:21:48 +02:00
owenca
e7aed737eb [clang-format][NFC] Add a few regression tests 2022-05-05 15:12:24 -07:00
owenca
8b626a2caa [clang-format] Fix another bug in AlignConsecutiveAssignments
The ShouldShiftBeAdded lambda checks if extra space should be
added before the wrapped part of a braced list. If the first
element of the list is wrapped, no extra space should be added.

Fixes #55161.

Differential Revision: https://reviews.llvm.org/D124956
2022-05-05 14:48:56 -07:00
Sam McCall
04b4190489 [Driver] Make "upgrade" of -include to include-pch optional; disable in clangd
If clang is passed "-include foo.h", it will rewrite to "-include-pch foo.h.pch"
before passing it to cc1, if foo.h.pch exists.

Existence is checked, but validity is not. This is probably a reasonable
assumption for the compiler itself, but not for clang-based tools where the
actual compiler may be a different version of clang, or even GCC.
In the end, we lose our -include, we gain a -include-pch that can't be used,
and the file often fails to parse.

I would like to turn this off for all non-clang invocations (i.e.
createInvocationFromCommandLine), but we have explicit tests of this behavior
for libclang and I can't work out the implications of changing it.

Instead this patch:
 - makes it optional in the driver, default on (no change)
 - makes it optional in createInvocationFromCommandLine, default on (no change)
 - changes driver to do IO through the VFS so it can be tested
 - tests the option
 - turns the option off in clangd where the problem was reported

Subsequent patches should make libclang opt in explicitly and flip the default
for all other tools. It's probably also time to extract an options struct
for createInvocationFromCommandLine.

Fixes https://github.com/clangd/clangd/issues/856
Fixes https://github.com/clangd/vscode-clangd/issues/324

Differential Revision: https://reviews.llvm.org/D124970
2022-05-05 16:47:17 +02:00
Sam McCall
40c13720a4 [Frontend] give createInvocationFromCommandLine an options struct
It's accumulating way too many optional params (see D124970)

While here, improve the name and the documentation.

Differential Revision: https://reviews.llvm.org/D124971
2022-05-05 15:12:07 +02:00
Eric Li
58abe36ae7 [clang][dataflow] Add flowConditionIsTautology function
Provide a way for users to check if a flow condition is
unconditionally true.

Differential Revision: https://reviews.llvm.org/D124943
2022-05-05 03:57:43 +00:00
owenca
342e1c77c3 [clang-format] Fix a bug in AlignConsecutiveAssignments
Fixes #55113.

Differential Revision: https://reviews.llvm.org/D124868
2022-05-04 12:04:43 -07:00
Cyndy Ishida
b6c67c3c67 [clang] Track how headers get included generally during lookup time
tapi & clang-extractapi both attempt to construct then check against
how a header was included to determine api information when working
against multiple search paths, headermap, and vfsoverlay mechanisms.
Validating this against what the preprocessor sees during lookup time
makes this check more reliable.

Reviewed By: zixuw, jansvoboda11

Differential Revision: https://reviews.llvm.org/D124638
2022-05-04 09:52:31 -07:00
Eric Li
62b2a47a9f [clang][dataflow] Only skip ExprWithCleanups when visiting terminators
`IgnoreParenImpCasts` will remove implicit casts to bool
(e.g. `PointerToBoolean`), such that the resulting expression may not
be of the `bool` type. The `cast_or_null<BoolValue>` in
`extendFlowCondition` will then trigger an assert, as the pointer
expression will not have a `BoolValue`.

Instead, we only skip `ExprWithCleanups` and `ParenExpr` nodes, as the
CFG does not emit them.

Differential Revision: https://reviews.llvm.org/D124807
2022-05-04 15:31:49 +00:00
Aaron Ballman
2cb2cd242c Change the behavior of implicit int diagnostics
C89 allowed a type specifier to be elided with the resulting type being
int, aka implicit int behavior. This feature was subsequently removed
in C99 without a deprecation period, so implementations continued to
support the feature. Now, as with implicit function declarations, is a
good time to reevaluate the need for this support.

This patch allows -Wimplicit-int to issue warnings in C89 mode (off by
default), defaults the warning to an error in C99 through C17, and
disables support for the feature entirely in C2x. It also removes a
warning about missing declaration specifiers that really was just an
implicit int warning in disguise and other minor related cleanups.
2022-05-04 08:35:47 -04:00
Fred Tingaud
ad47114ad8 In MSVC compatibility mode, friend function declarations behave as function declarations
Before C++20, MSVC treated any friend function declaration as a function declaration, so the following code would compile despite funGlob being declared after its first call:

```
class Glob {
public:
  friend void funGlob();

  void test() {
    funGlob();
  }
};

void funGlob() {}
```
This proposed patch mimics the MSVC behavior when in MSVC compatibility mode

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D124613
2022-05-03 11:31:50 +02:00
Marek Kurdej
c819dce2d3 [clang-format] Add a regression test for aligning macros with keywords.
Test from issue https://github.com/llvm/llvm-project/issues/54953.
2022-05-03 11:09:38 +02:00
owenca
c8603db071 [clang-format] Fix a bug that misformats Access Specifier after *[]
Fixes #55132.

Differential Revision: https://reviews.llvm.org/D124589
2022-05-02 01:39:26 -07:00
Stanislav Gatev
955a05a278 [clang][dataflow] Optimize flow condition representation
Enable efficient implementation of context-aware joining of distinct
boolean values. It can be used to join distinct boolean values while
preserving flow condition information.

Flow conditions are represented as Token <=> Clause iff formulas. To
perform context-aware joining, one can simply add the tokens of flow
conditions to the formula when joining distinct boolean values, e.g:
`makeOr(makeAnd(FC1, Val1), makeAnd(FC2, Val2))`. This significantly
simplifies the implementation of `Environment::join`.

This patch removes the `DataflowAnalysisContext::getSolver` method.
The `DataflowAnalysisContext::flowConditionImplies` method should be
used instead.

Reviewed-by: ymandel, xazax.hun

Differential Revision: https://reviews.llvm.org/D124395
2022-05-01 16:25:29 +00:00
NAKAMURA Takumi
a60ef98bb1 ClangDriverTests:ToolChainTest.cpp: Fix warnings. [-Wsign-compare]
EXPECT_EQ(num,num) is aware of signedness, even if rhs is a constant.
2022-04-30 17:19:26 +09:00
python3kgae
73417c5176 [HLSL][clang][Driver] Support validator version command line option.
The DXIL validator version option(/validator-version) decide the validator version when compile hlsl.
The format is major.minor like 1.0.

In normal case, the value of validator version should be got from DXIL validator. Before we got DXIL validator ready for llvm/main, DXIL validator version option is added first to set validator version.

It will affect code generation for DXIL, so it is treated as a code gen option.

A new member std::string DxilValidatorVersion is added to clang::CodeGenOptions.

Then CGHLSLRuntime is added to clang::CodeGenModule.
It is used to translate clang::CodeGenOptions::DxilValidatorVersion into a ModuleFlag under key "dx.valver" at end of clang code generation.

Reviewed By: beanz

Differential Revision: https://reviews.llvm.org/D123884
2022-04-29 16:48:08 -07:00
Bill Wendling
6f79700830 [randstruct] Automatically randomize a structure of function pointers
Strutures of function pointers are a good surface area for attacks. We
should therefore randomize them unless explicitly told not to.

Reviewed By: aaron.ballman, MaskRay

Differential Revision: https://reviews.llvm.org/D123544
2022-04-29 11:05:09 -07:00
Argyrios Kyrtzidis
42823beb1d [Tooling/DependencyScanning] Make skipping excluded PP ranges during dependency scanning the default
This is to improve maintenance a bit and remove need to maintain the additional option and related code-paths.

Differential Revision: https://reviews.llvm.org/D124558
2022-04-28 15:23:03 -07:00
Bill Wendling
463790bfc7 [randstruct] Randomize all elements of a record
A record may have more than just FieldDecls in it. If so, then we're
likely to drop them if we only randomize the FieldDecls.

We need to be careful about anonymous structs/unions. Their fields are
made available in the RecordDecl as IndirectFieldDecls, which are listed
after the anonymous struct/union. The ordering doesn't appear to be
super important, however we place them unrandomized at the end of the
RecordDecl just in case. There's also the possiblity of
StaticAssertDecls. We also want those at the end.

All other non-FieldDecls we place at the top, just in case we get
something like:

    struct foo {
      enum e { BORK };
      enum e a;
    };

Link: https://github.com/KSPP/linux/issues/185

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D123958
2022-04-28 12:01:11 -07:00
Ilya Biryukov
2b833d4086 [AST] Improve traversal of concepts and concept requirements
- Do not traverse concept decl inside `AutoType`. We only traverse
  declaration and definitions, not references to a declaration.
- Do not visit implicit AST node the relevant traversal mode.
- Add traversal extension points for concept requirements.
- Renamed `TraverseConceptReference` to mark as helper to share
  the code. Having an extension point there seems confusing given that
  there are many concept refences in the AST that do not call the
  helper. Those are `AutoType`, `AutoTypeLoc` and constraint requirements.

Only clangd code requires an update.

There are no use-cases for concept requirement traversals yet, but
I added them in the earlier version of the patch and decided to keep
them for completeness.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D124532
2022-04-28 09:33:26 +00:00
Krasimir Georgiev
be656df187 [clang-format] add a regression test for include sorting
This adds a regression test from the comments
on https://reviews.llvm.org/D121370.

Reviewed By: MyDeveloperDay, curdeius

Differential Revision: https://reviews.llvm.org/D124513
2022-04-28 11:02:14 +02:00
Krasimir Georgiev
e8cc7490d2 Revert "[clang-format] SortIncludes should support "@import" lines in Objective-C"
This reverts commit d46fa023ca.
Regressed include order in some cases with trailing comments, see the
comments on https://reviews.llvm.org/D121370. Will add a regression test
in a follow-up commit.
2022-04-28 11:00:32 +02:00
owenca
db57acff26 [clang-format] Adjust editor cursor position past #include blocks
Fixes #55027.

Differential Revision: https://reviews.llvm.org/D124452
2022-04-27 14:26:23 -07:00
Haojian Wu
8052f4d22a [AST] Consider QualifiedTemplateName in TemplateName::getAsUsingDecl().
If the underlying template name of a qualified template name is a using
decl, TemplateName::getAsUsingDecl() will return it.

This will make the UsingTemplateName consumer life easier.

Differential Revision: https://reviews.llvm.org/D124437
2022-04-27 14:16:19 +02:00
Ishaan Gandhi
87468e85fc compile commands header to source heuristic lower-cases filenames before inferring file types
This leads to ".C" files being rewritten as ".c" files and being inferred to be "c" files as opposed to "c++" files.

Fixes https://github.com/clangd/clangd/issues/1108

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D124262
2022-04-25 20:40:56 +02:00
Yitzhak Mandelbaum
37b4782e3e [clang][dataflow] Fix Environment::join's handling of flow-condition merging.
The current implementation mutates the environment as it performs the
join. However, that interferes with the call to the model's `merge` operation,
which can modify `MergedEnv`. Since any modifications are assumed to apply to
the post-join environment, providing the same environment for both is
incorrect. This mismatch is a particular concern for joining the flow
conditions, where modifications in the old environment may not be propagated to
the new one.

Differential Revision: https://reviews.llvm.org/D124104
2022-04-25 15:05:50 +00:00
sstwcw
c261f78d05 [clang-format] Refactor determineStarAmpUsage NFC
There was some duplicate code in determineStarAmpUsage and
determinePlusMinusCaretUsage

Now a `-` or `+` following `;`, `sizeof`, `co_await`, or `delete` is
regarded as a unary operator.

Now a `*` or `&` following `case` is also a unary operator.

Reviewed By: curdeius, MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D121754
2022-04-24 22:30:22 +00:00
Volodymyr Sapsai
4604db9493 [ASTStructuralEquivalence] Add support for comparing ObjCCategoryDecl.
Differential Revision: https://reviews.llvm.org/D121176
2022-04-22 16:51:19 -07:00
Tom Eccles
225b91e6cb Fix crash getting name of a template decl
NamedDecl::getIdentifier can return a nullptr when
DeclarationName::isIdentifier is false, which leads to a null pointer
dereference when TypePrinter::printTemplateId calls ->getName().

NamedDecl::getName does the same thing in the successful case and
returns an empty string in the failure case.

This crash affects the llvm 14 packages on llvm.org.
2022-04-22 13:03:28 -04:00
Nico Weber
8dbc6b5600 Revert "[randstruct] Check final randomized layout ordering"
This reverts commit a7815d33bf.
Test fails on Windows, see comments on https://reviews.llvm.org/D124199
2022-04-22 08:27:32 -04:00
Bill Wendling
a7815d33bf [randstruct] Check final randomized layout ordering
This uses "llvm::shuffle" to stop differences in shuffle ordering on
different platforms.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D124199
2022-04-21 19:41:00 -07:00
owenca
221c2b68dd [clang-format] Fix a crash on AllowShortFunctionsOnASingleLine
Fixes #55008.

Differential Revision: https://reviews.llvm.org/D124152
2022-04-21 14:56:30 -07:00
Sam McCall
a7691dee2d [Testing] TestAST, a helper for writing straight-line AST tests
Tests that need ASTs have to deal with the awkward control flow of
FrontendAction in some way. There are a few idioms used:
 - don't bother with unit tests, use clang -dump-ast
 - create an ASTConsumer by hand, which is bulky
 - use ASTMatchFinder - works pretty well if matchers are actually
   needed, very strange if they are not
 - use ASTUnit - this yields nice straight-line code, but ASTUnit is a
   terrifically complicated library not designed for this purpose

TestAST provides a very simple way to write straight-line tests: specify
the code/flags and it provides an AST that is kept alive until the
object is destroyed.
It's loosely modeled after TestTU in clangd, which we've successfully
used for a variety of tests.

I've updated a couple of clang tests to use this helper, IMO they're clearer.

Differential Revision: https://reviews.llvm.org/D123668
2022-04-21 21:46:45 +02:00
Haojian Wu
1234b1c6d8 [AST] Support template declaration found through using-decl for QualifiedTemplateName.
This is a followup of https://reviews.llvm.org/D123127, adding support
for the QualifiedTemplateName.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D123775
2022-04-21 10:53:23 +02:00
Richard Smith
72315d02c4 Treat std::move, forward, etc. as builtins.
This is extended to all `std::` functions that take a reference to a
value and return a reference (or pointer) to that same value: `move`,
`forward`, `move_if_noexcept`, `as_const`, `addressof`, and the
libstdc++-specific function `__addressof`.

We still require these functions to be declared before they can be used,
but don't instantiate their definitions unless their addresses are
taken. Instead, code generation, constant evaluation, and static
analysis are given direct knowledge of their effect.

This change aims to reduce various costs associated with these functions
-- per-instantiation memory costs, compile time and memory costs due to
creating out-of-line copies and inlining them, code size at -O0, and so
on -- so that they are not substantially more expensive than a cast.
Most of these improvements are very small, but I measured a 3% decrease
in -O0 object file size for a simple C++ source file using the standard
library after this change.

We now automatically infer the `const` and `nothrow` attributes on these
now-builtin functions, in particular meaning that we get a warning for
an unused call to one of these functions.

In C++20 onwards, we disallow taking the addresses of these functions,
per the C++20 "addressable function" rule. In earlier language modes, a
compatibility warning is produced but the address can still be taken.

The same infrastructure is extended to the existing MSVC builtin
`__GetExceptionInfo`, which is now only recognized in namespace `std`
like it always should have been.

This is a re-commit of
  fc30901096,
  a571f82a50,
  64c045e25b, and
  de6ddaeef3,
and reverts aa643f455a.
This change also includes a workaround for users using libc++ 3.1 and
earlier (!!), as apparently happens on AIX, where std::move sometimes
returns by value.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D123345

Revert "Fixup D123950 to address revert of D123345"

This reverts commit aa643f455a.
2022-04-20 17:58:31 -07:00
David Tenty
98d911e01f Revert "Treat std::move, forward, etc. as builtins."
This reverts commit b27430f9f4 as the
    parent https://reviews.llvm.org/D123345 breaks the AIX CI:

    https://lab.llvm.org/buildbot/#/builders/214/builds/819
2022-04-20 19:14:37 -04:00