Commit Graph

4567 Commits

Author SHA1 Message Date
Vassil Vassilev 44a4000181 [clang-repl] Land initial infrastructure for incremental parsing
In http://lists.llvm.org/pipermail/llvm-dev/2020-July/143257.html we have
mentioned our plans to make some of the incremental compilation facilities
available in llvm mainline.

This patch proposes a minimal version of a repl, clang-repl, which enables
interpreter-like interaction for C++. For instance:

./bin/clang-repl
clang-repl> int i = 42;
clang-repl> extern "C" int printf(const char*,...);
clang-repl> auto r1 = printf("i=%d\n", i);
i=42
clang-repl> quit

The patch allows very limited functionality, for example, it crashes on invalid
C++. The design of the proposed patch follows closely the design of cling. The
idea is to gather feedback and gradually evolve both clang-repl and cling to
what the community agrees upon.

The IncrementalParser class is responsible for driving the clang parser and
codegen and allows the compiler infrastructure to process more than one input.
Every input adds to the “ever-growing” translation unit. That model is enabled
by an IncrementalAction which prevents teardown when HandleTranslationUnit.

The IncrementalExecutor class hides some of the underlying implementation
details of the concrete JIT infrastructure. It exposes the minimal set of
functionality required by our incremental compiler/interpreter.

The Transaction class keeps track of the AST and the LLVM IR for each
incremental input. That tracking information will be later used to implement
error recovery.

The Interpreter class orchestrates the IncrementalParser and the
IncrementalExecutor to model interpreter-like behavior. It provides the public
API which can be used (in future) when using the interpreter library.

Differential revision: https://reviews.llvm.org/D96033
2021-05-13 04:23:24 +00:00
Pratyush Das 99d63ccff0 Add type information to integral template argument if required.
Non-comprehensive list of cases:
 * Dumping template arguments;
 * Corresponding parameter contains a deduced type;
 * Template arguments are for a DeclRefExpr that hadMultipleCandidates()

Type information is added in the form of prefixes (u8, u, U, L),
suffixes (U, L, UL, LL, ULL) or explicit casts to printed integral template
argument, if MSVC codeview mode is disabled.

Differential revision: https://reviews.llvm.org/D77598
2021-05-12 19:00:08 +00:00
Weston Carvalho 1f65f42dd3 Make `hasTypeLoc` matcher support more node types.
Differential Revision: https://reviews.llvm.org/D101572
2021-05-08 00:35:22 +01:00
Eliza Velasquez ec725b307f [clang-format] Fix C# nullable-related errors
This fixes two errors:

Previously, clang-format was splitting up type identifiers from the
nullable ?. This changes this behavior so that the type name sticks with
the operator.

Additionally, nullable operators attached to return types in interface
functions were not parsed correctly. Digging deeper, it looks like
interface bodies were being parsed differently than classes and structs,
causing MustBeDeclaration to be incorrect for interface members. They
now share the same logic.

One other change is reintroducing the CSharpNullable type independent of
JsTypeOptionalQuestion. Despite having a similar semantic purpose, their
actual syntax differs quite a bit.

Reviewed By: MyDeveloperDay, curdeius

Differential Revision: https://reviews.llvm.org/D101860
2021-05-06 12:11:15 +02:00
Eliza Velasquez a437befa8f [clang-format] Add more support for C# 8 nullables
This adds support for the null-coalescing assignment and null-forgiving
operators.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving

Reviewed By: krasimir, curdeius

Differential Revision: https://reviews.llvm.org/D101702
2021-05-06 11:58:38 +02:00
Nathan James 61dc0f2b59
[Format] Don't sort includes if DisableFormat is true
Fixes https://llvm.org/PR35099.

I'm not sure if this decision was intentional but its definitely confusing for users.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D101628
2021-05-04 19:04:12 +01:00
Nico Weber d7ec48d71b [clang] accept -fsanitize-ignorelist= in addition to -fsanitize-blacklist=
Use that for internal names (including the default ignorelists of the
sanitizers).

Differential Revision: https://reviews.llvm.org/D101832
2021-05-04 10:24:00 -04:00
Luis Penagos 8fa56f7ede [clang-format] Prevent extraneous space insertion in bitshift operators
This serves to augment the improvements made in https://reviews.llvm.org/D86581. It prevents clang-format from interpreting bitshift operators as template arguments in certain circumstances. This is an attempt at fixing https://bugs.llvm.org/show_bug.cgi?id=49868

Reviewed By: MyDeveloperDay, krasimir

Differential Revision: https://reviews.llvm.org/D100778
2021-05-04 12:28:49 +02:00
Marek Kurdej 8d93d7ffed [clang-format] Add options to AllowShortIfStatementsOnASingleLine to apply to "else if" and "else".
This fixes the bug http://llvm.org/pr50019.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D100727
2021-05-03 18:11:25 +02:00
Martin Probst b2780cd744 clang-format: [JS] handle "off" in imports
Previously, the JavaScript import sorter would ignore `// clang-format
off` and `on` comments. This change fixes that. It tracks whether
formatting is enabled for a stretch of imports, and then only sorts and
merges the imports where formatting is enabled, in individual chunks.

This means that there's no meaningful total order when module references are mixed
with blocks that have formatting disabled. The alternative approach
would have been to sort all imports that have formatting enabled in one
group. However that raises the question where to insert the
formatting-off block, which can also impact symbol visibility (in
particular for exports). In practice, sorting in chunks probably isn't a
big problem.

This change also simplifies the general algorithm: instead of tracking
indices separately and sorting them, it just sorts the vector of module
references. And instead of attempting to do fine grained tracking of
whether the code changed order, it just prints out the module references
text, and compares that to the previous text. Given that source files
typically have dozens, but not even hundreds of imports, the performance
impact seems negligible.

Differential Revision: https://reviews.llvm.org/D101515
2021-04-30 14:18:52 +02:00
Marek Kurdej 9363aa90bf [clang-format] Add `SpacesInAngles: Leave` option to keep spacing inside angle brackets as is.
A need for such an option came up in a few libc++ reviews. That's because libc++ has both code in C++03 and newer standards.
Currently, it uses `Standard: C++03` setting for clang-format, but this breaks e.g. u8"string" literals.
Also, angle brackets are the only place where C++03-specific formatting needs to be applied.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D101344
2021-04-29 08:58:50 +02:00
Nico Weber 0f1137ba79 [clang/Basic] Make TargetInfo.h not use DataLayout again
Reverts parts of https://reviews.llvm.org/D17183, but keeps the
resetDataLayout() API and adds an assert that checks that datalayout string and
user label prefix are in sync.

Approach 1 in https://reviews.llvm.org/D17183#2653279
Reduces number of TUs build for 'clang-format' from 689 to 575.

I also implemented approach 2 in D100764. If someone feels motivated
to make us use DataLayout more, it's easy to revert this change here
and go with D100764 instead. I don't plan on doing more work in this
area though, so I prefer going with the smaller, more self-consistent change.

Differential Revision: https://reviews.llvm.org/D100776
2021-04-27 22:26:10 -04:00
Marek Kurdej 3feb84a36f [clang-format] Merge SpacesInAngles tests. NFC. 2021-04-27 09:32:09 +02:00
Stephen Kelly 50b523cb2c [AST] Fix DeclarationNameInfo introspection
Some AST classes return `const DeclarationNameInfo &` instead of
returning by value (eg CXXDependentScopeMemberExpr).
2021-04-26 18:49:13 +01:00
Krasimir Georgiev 5987d7c59d [clang-format] fix indent in alignChainedConditionals
Clang-format was indenting the lines following the `?` in the added test
case by +5 instead of +4. This only happens in a very specific
situation, where the `?` is followed by a multiline block comment, as in
the example. This fix addresses this without regressing any of the
existing tests.

Differential Revision: https://reviews.llvm.org/D101033
2021-04-26 11:06:29 +02:00
Stephen Kelly a9676febb9 [AST] Add DeclarationNameInfo to node introspection
Differential Revision: https://reviews.llvm.org/D101049
2021-04-25 12:12:03 +01:00
Stephen Kelly df82fa8d9b [AST] Update tests to query for introspection support 2021-04-23 17:51:10 +01:00
Stephen Kelly 21ce124e1e [AST] Add NestedNameSpecifierLoc accessors to node introspection
Differential Revision: https://reviews.llvm.org/D100712
2021-04-22 11:27:19 +01:00
Martin Probst fbc6f42dbe clang-format: [JS] do not merge side-effect imports.
The if condition was testing the current element, but
forgot to check the previous element (doh), so it
would fail depending on sort order of the imports.

Differential Revision: https://reviews.llvm.org/D101020
2021-04-22 10:36:47 +02:00
Martin Probst 70ae843d99 clang-format: [JS] do not wrap after `asserts`
`asserts` is a pseudo keyword in TypeScript used in return types.
Wrapping after it triggers automatic semicolon insertion, which
breaks the code semantics/syntax.

`asserts` is different from other pseudo keywords in that it is
specific to TS and only carries meaning in a very specific location.
Thus introducing a token type is probably overkill.

Differential Revision: https://reviews.llvm.org/D100953
2021-04-21 16:33:55 +02:00
Martin Probst 3d4a6037ff clang-format: [JS] do not merge imports and exports.
Previously, clang-format would erroneously merge import and export
statements. These need to be kept separate, as the semantics differ.

Differential Revision: https://reviews.llvm.org/D100752
2021-04-20 13:08:18 +02:00
Stephen Kelly 782c3e23ba [AST] Fix comparison to of SourceRanges in container
Differential Revision: https://reviews.llvm.org/D100723
2021-04-19 21:19:21 +01:00
Stephen Kelly abacaef181 [AST] Update introspection API to use const-ref for copyable types
Differential Revision: https://reviews.llvm.org/D100720
2021-04-19 21:07:47 +01:00
Jan Svoboda 26bbb8700b [clang] Implement CompilerInvocation copy assignment
This patch implements the copy assignment for `CompilerInvocation`.

Eventually, the deep-copy operation will be moved into a `clone()` method (D100460), but until then, this is necessary for basic ergonomics.

Depends on D100455.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D100473
2021-04-19 11:12:22 +02:00
Stephen Kelly dd68942f1d [AST] Add TypeLoc support to node introspection
Extend the matchers gathering API for types to record template
parameters.  The TypeLoc type hierarchy has some types which are
templates used in CRTP such as PointerLikeTypeLoc.  Record the inherited
template and template arguments of types inheriting those CRTP types in
the ClassInheritance map.  Because the name inherited from is now
computed, the value type in that map changes from StringRef to
std::string.  This also causes the toJSON override signature used to
serialize that map to change.

Remove the logic for skipping over empty ClassData instances.  Several
classes such as TypeOfExprTypeLoc inherit a CRTP class which provides
interesting locations though the derived class does not.  Record it as a
class to make the locations it inherits available.

Record the typeSourceInfo accessors too as they provide access to
TypeLocs in many classes.

The existing unit tests use UnorderedElementsAre to compare the
introspection result with the expected result.  Our current
implementation of google mock (in gmock-generated-matchers.h) is limited
to support for comparing a container of 10 elements.  As we are now
returning more than 10 results for one of the introspection tests,
change it to instead compare against an ordered vector of pairs.

Because a macro is used to generate API strings and API calls, disable
clang-format in blocks of expected results.  Otherwise clang-format
would insert whitespaces which would then be compared against the
introspected strings and fail the test.

Introduce a recursion guard in the generated code.  The TypeLoc class
has IgnoreParens() API which by default returns itself, so it would
otherwise recurse infinitely.

Differential Revision: https://reviews.llvm.org/D100516
2021-04-17 22:58:02 +01:00
Stephen Kelly ebc6608fb7 [AST] Remove args from LocationCall
This class initially had args to be generic to future needs. In
particular, I thought that source location introspection should show the
getBeginLoc of CallExpr args and the getArgLoc of
TemplateSpecializationLocInfo etc.  However, that is probably best left
out of source location introspection because it involves node traversal.

If something like this is needed in the future, it can be added in the
future.

Differential Revision: https://reviews.llvm.org/D100688
2021-04-17 17:21:55 +01:00
Max Sagebaum fd4e08aa8f [clang-format] Inconsistent behavior regarding line break before access modifier
Fixes https://llvm.org/PR41870.

Checks for newlines in option Style.EmptyLineBeforeAccessModifier are now based on the formatted new lines and not on the new lines in the file.

Reviewed By: HazardyKnusperkeks, curdeius

Differential Revision: https://reviews.llvm.org/D99503
2021-04-16 10:39:13 +02:00
Stephen Kelly f62ad15cd7 NFC: Add a simple test for introspection call formatting 2021-04-15 23:45:54 +01:00
Stephen Kelly be65347326 NFC: Add missing matcher for test method
The intention is to match the definition.
2021-04-15 23:26:00 +01:00
Stephen Kelly 4f6d698467 [AST] Fix location call storage with common last-invocation
Differential Revision: https://reviews.llvm.org/D100548
2021-04-15 23:15:11 +01:00
Nathan James f019e5f73e
[AST][Introspection] Add a check to detect if introspection is supported.
This could probably be made into a compile time constant, but that would involve generating a second inc file.

Reviewed By: steveire

Differential Revision: https://reviews.llvm.org/D100530
2021-04-15 22:21:41 +01:00
Nathan James 542e7806e6
[AST] Add a print method to Introspection LocationCall
Add a print method that takes a raw_ostream.
Change LocationCallFormatterCpp::format to call that method.

Reviewed By: steveire

Differential Revision: https://reviews.llvm.org/D100423
2021-04-15 22:18:29 +01:00
Max Sagebaum dda978eef8 [clang-format] Option for empty lines after an access modifier.
The current logic for access modifiers in classes ignores the option 'MaxEmptyLinesToKeep=1'. It is therefore impossible to have a coding style that requests one empty line after an access modifier. The patch allows the user to configure how many empty lines clang-format should add after an access modifier. This will remove lines if there are to many and will add them if there are missing.

Reviewed By: MyDeveloperDay, curdeius

Differential Revision: https://reviews.llvm.org/D98237
2021-04-15 21:03:07 +02:00
Stephen Kelly f347f0e0b8 [AST] Add introspection support for more base nodes
Fix the logic of detecting pseudo-virtual getBeginLoc etc on Stmt and
Decl subclasses.

Adjust the test infrastructure to filter out invalid source locations.
This makes the tests more clear about which nodes have which locations.

Differential Revision: https://reviews.llvm.org/D99231
2021-04-14 21:31:23 +01:00
Shu-Chun Weng 1c5717225e [libTooling] Add smart pointer support to the `access` Stencil
This extends smart pointer support beyond the existing `maybeDeref` and
`maybeAddressOf`.

Differential Revision: https://reviews.llvm.org/D100450
2021-04-14 10:45:59 -07:00
Martin Probst 4d195f1b4d review comments
track symbol merge status in references to avoid excesive rewrites
2021-04-14 17:20:08 +02:00
Martin Probst d45df0d29f clang-format: [JS] merge import lines.
Multiple lines importing from the same URL can be merged:

    import {X} from 'a';
    import {Y} from 'a';

Merge to:

    import {X, Y} from 'a';

This change implements this merge operation. It takes care not to merge in
various corner case situations (default imports, star imports).

Differential Revision: https://reviews.llvm.org/D100466
2021-04-14 17:20:07 +02:00
Jan Svoboda 09d1f6e6b7 [clang] Fix copy constructor of CompilerInvocation
The `CompilerInvocationBase` class factors out members of `CompilerInvocation` that need special handling (initialization or copy constructor), so that `CompilerInvocation` can be implemented as a simple value object.

Currently, the `AnalyzerOpts` member of `CompilerInvocation` violates that setup. This patch extracts the member to `CompilerInvocationBase` and handles it in the copy constructor the same way other it handles other members.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D99568
2021-04-14 09:13:35 +02:00
Daniele Castagna 7dd6068899
[clang-rename] Handle designated initializers.
clang Tooling, and more specifically Refactoring/Rename, have support
code to extract source locations given a Unified Symbol Resolution set.
This support code is used by clang-rename and other tools that might not
be in the tree.

Currently field designated initializer are not supported.
So, renaming S::a to S::b in this code:

  S s = { .a = 10 };

will not extract the field designated initializer for a (the 'a' after the
dot).

This patch adds support for field designated initialized to
RecursiveSymbolVisitor and RenameLocFinder that is used in
createRenameAtomicChanges.

Differential Revision: https://reviews.llvm.org/D100310
2021-04-12 13:15:14 -07:00
Balázs Kéri 6e51991049 [clang][AST] Handle overload callee type in CallExpr::getCallReturnType.
The function did not handle every case. In some cases this
caused assertion failure.
After the fix the function returns DependentTy if the exact
return type can not be determined.

It seems that clang itself does not call the function in the
affected cases but some checker or other code may call it.

Reviewed By: hokein

Differential Revision: https://reviews.llvm.org/D95244
2021-04-12 09:44:17 +02:00
Whisperity 3b677b81ce [libtooling][clang-tidy] Fix diagnostics not highlighting fed SourceRanges
Fixes bug http://bugs.llvm.org/show_bug.cgi?id=49000.

This patch allows Clang-Tidy checks to do

    diag(X->getLocation(), "text") << Y->getSourceRange();

and get the highlight of `Y` as expected:

    warning: text [blah-blah]
        xxx(something)
        ^   ~~~~~~~~~

Reviewed-By: aaron.ballman, njames93

Differential Revision: http://reviews.llvm.org/D98635
2021-04-10 16:43:44 +02:00
Nikita Kniazev 2f181086b5 [ASTMatchers] Add `cxxBaseSpecifier` matcher (non-top-level)
Required for capturing base specifier in matchers:
  `cxxRecordDecl(hasDirectBase(cxxBaseSpecifier().bind("base")))`

Reviewed By: steveire, aaron.ballman

Differential Revision: https://reviews.llvm.org/D69218
2021-04-09 00:05:36 +01:00
Ben Langmuir 93c87fc06e [index] Improve macro indexing support
The major change here is to index macro occurrences in more places than
before, specifically

* In non-expansion references such as `#if`, `#ifdef`, etc.
* When the macro is a reference to a builtin macro such as __LINE__.
* When using the preprocessor state instead of callbacks, we now include
  all definition locations and undefinitions instead of just the latest
  one (which may also have had the wrong location previously).
* When indexing an existing module file (.pcm), we now include module
  macros, and we no longer report unrelated preprocessor macros during
  indexing the module, which could have caused duplication.

Additionally, we now correctly obey the system symbol filter for macros,
so by default in system headers only definition/undefinition occurrences
are reported, but it can be configured to report references as well if
desired.

Extends FileIndexRecord to support occurrences of macros. Since the
design of this type is to keep a single list of entities organized by
source location, we incorporate macros into the existing DeclOccurrence
struct.

Differential Revision: https://reviews.llvm.org/D99758
2021-04-06 09:12:14 -07:00
Simon Pilgrim 2901dc7575 Don't directly dereference getAs<> casts to avoid potential null dereferences. NFCI.
Replace with castAs<> which asserts the cast is valid.

Fixes a number of static analyzer warnings.
2021-04-06 12:24:19 +01:00
Jan Svoboda 2935737da3 [clang][tooling] Create SourceManager for DiagnosticsEngine before command-line parsing
In D84673, we started using `DiagnosticsEngine` during command-line parsing in more contexts.

When using `ToolInvocation`, a custom `DiagnosticsConsumer` can be specified and it might expect `SourceManager` to be present on the emitted diagnostics.

This patch ensures the `SourceManager` is set up in such scenarios.

Test authored by Jordan Rupprecht.

Reviewed By: rupprecht

Differential Revision: https://reviews.llvm.org/D99414
2021-04-06 10:40:47 +02:00
Jan Svoboda cc26943313 [clang][cli] Ensure plugin args are generated in deterministic order
The '-plugin-arg' command-line arguments are not being generated in deterministic order.

This patch changes the storage from `std::unordered_map` to `std::map` to enforce ordering.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D99879
2021-04-06 09:24:42 +02:00
oToToT a89fb29398
[clang][ItaniumMangle] Check SizeExpr for DependentSizedArrayType
(PR49478)

As ArrayType::ArrayType mentioned in clang/lib/AST/Type.cpp, a
DependentSizedArrayType might not have size expression because it it
used as the type of a dependent array of unknown bound with a dependent
braced initializer.

Thus, I add a check when mangling array of that type.

This should fix https://bugs.llvm.org/show_bug.cgi?id=49478

Reviewed By: Richard Smith - zygoloid

Differential Revision: https://reviews.llvm.org/D99407
2021-04-02 15:31:20 +08:00
Balazs Benics 9d474be11d [ASTImporter][NFC] Fix duplicated symbols in "Improve test coverage"
D99576 introduced a duplicate symbol, now im removing it.

Differential Revision: https://reviews.llvm.org/D99576
2021-03-31 12:47:50 +02:00
Balazs Benics 936d1e97a3 [ASTImporter][NFC] Improve test coverage
All three cases were imported correctly.
For BlockDecls, correctly means that we don't support importing them, thus an
error is the expected behaviour.

 - BlockDecls were not yet covered. I know that they are not imported but the
   test at least documents it.
 - Default values for ParmVarDecls were also uncovered.
 - Importing bitfield FieldDecls were imported correctly.

Reviewed By: martong, shafik

Differential Revision: https://reviews.llvm.org/D99576
2021-03-31 12:10:23 +02:00
Nico Rieck bc4b0fc53e [clang-format] Fix east const pointer alignment of operators
This patch fixes left pointer alignment after pointer qualifiers of
operators. Currently "operator void const*()" is formatted with a space between
const and pointer despite setting PointerAlignment to Left.

AFAICS this has been broken since clang-format 10.

Reviewed By: MyDeveloperDay, curdeius

Differential Revision: https://reviews.llvm.org/D99458
2021-03-30 17:18:32 +02:00