Commit Graph

142 Commits

Author SHA1 Message Date
Doug Gregor
200f2340d9 [Macros] Be deliberate about walking macro arguments vs. expansions
Provide ASTWalker with a customization point to specify whether to
check macro arguments (which are type checked but never emitted), the
macro expansion (which is the result of applying the macro and is
actually emitted into the source), or both. Provide answers for the
~115 different ASTWalker visitors throughout the code base.

Fixes rdar://104042945, which concerns checking of effects in
macro arguments---which we shouldn't do.
2023-02-28 17:48:23 -08:00
Ben Barham
0c3f538822 [AST] Allow ignoring macro expansions
Various requests expect to be walking over the current source file.
While we could add checks to all these to skip decls outside of the
current buffer, it's a little nicer to handle this during the walk
instead.

Allow ignoring nodes that are from macro expansions and add that flag to
the various walks that expect it.

Also add a new `getOriginalAttrs` that filters out attributes in
generated source.
2023-02-16 18:04:56 -08:00
Holly Borla
85f2a1a234 [Macros] Factor calls to ResolveMacroRequest into Decl::getResolvedMacro.
This prevents call-sites from making the mistake of passing an incorrect
DeclContext to the request, which can cause re-typechecking issues.
2023-02-15 18:52:02 -08:00
Holly Borla
f1953f311f [Macros] Don't invoke ResolveMacroRequest for the same custom attribute with
different DeclContexts.
2023-02-15 14:58:24 -08:00
Holly Borla
c33c925e81 [Macros] Remove the macro role argument to ResolveMacroRequest.
The macro role argument presented an opportunity for callers to accidentally
invoke this request twice for the same macro with slightly different macro
roles passed in, which resulted in re-typechecking the macro arguments.
Instead, derive the corresponding macro roles from the macro reference syntax.
2023-02-14 22:38:53 -08:00
Doug Gregor
96380624db [SourceKit] Implement macro expansion for (some) attached macros.
Extend the macro-expansion refactoring to work with member and
member-attribute attached macros. These expansions can return several
different changes, e.g., adding new members, sprinkling member
attributes around, and so on.
2023-02-11 11:23:28 -08:00
Anthony Latsis
1bb407ac8c [NFC] AST: Rename ComponentIdentTypeReprIdentTypeRepr 2023-01-07 07:15:36 +03:00
Doug Gregor
db801afbe9 Handle null macro references in the semantic annotator 2023-01-04 10:56:57 -08:00
Saleem Abdulrasool
7415cb8d5f Merge pull request #62455 from bnbarham/index-handle-implicit-conversions
[Index] Use `ide::getReferencedDecl` instead of custom `extractDecl`
2022-12-08 22:56:56 -05:00
Ben Barham
7fb229a5d5 [Index] Use ide::getReferencedDecl instead of custom extractDecl
`passReference` was failing to find the underlying declaration for a
constructor reference. In this case it was for a
`FunctionConversionExpr`, but there's likely others. Use
`ide::getReferencedDecl` instead.

Resolves rdar://102563191.
2022-12-08 09:16:23 -08:00
Rintaro Ishizaki
9f137074af [SourceKit/Refactoring] Macro expansion
Add refactoring "Expand Macro" that expands macro expressions.

rdar://102739026
2022-12-06 16:20:07 -08:00
Doug Gregor
c8819bd266 [Indexing] Index macro expansion expressions. 2022-11-28 18:33:10 -08:00
Anthony Latsis
18156d1177 Merge pull request #61347 from AnthonyLatsis/migrate-compiler-to-gh-issues
Gardening: Migrate compiler sources to GitHub issues
2022-09-30 10:48:52 +03:00
Anthony Latsis
2843e0c871 Gardening: Migrate compiler sources to GitHub issues 2022-09-29 23:58:55 +03:00
Hamish Knight
0a7b436c09 Use Action::Stop in more places
This used to be something we couldn't express in
certain walker methods. Update various places
where we can now use it.
2022-09-26 19:15:48 +01:00
Hamish Knight
4716f61fba [AST] Introduce explicit actions for ASTWalker
Replace the use of bool and pointer returns for
`walkToXXXPre`/`walkToXXXPost`, and instead use
explicit actions such as `Action::Continue(E)`,
`Action::SkipChildren(E)`, and `Action::Stop()`.
There are also conditional variants, e.g
`Action::SkipChildrenIf`, `Action::VisitChildrenIf`,
and `Action::StopIf`.

There is still more work that can be done here, in
particular:

- SourceEntityWalker still needs to be migrated.
- Some uses of `return false` in pre-visitation
methods can likely now be replaced by
`Action::Stop`.
- We still use bool and pointer returns internally
within the ASTWalker traversal, which could likely
be improved.

But I'm leaving those as future work for now as
this patch is already large enough.
2022-09-13 10:35:29 +01:00
Hamish Knight
6804623a82 Fix some nullptr dereferences
Turns out we were getting away with dereferencing
`nullptr` in a few cases as `walk` would use
`nullptr` to indicate that the walk should be
stopped, and luckily Clang didn't optimize it to
something broken.

This commit is fairly defensive and sprinkles
some null checks for calls to `walk` directly
on a body of a function or top-level code decl.
2022-09-13 10:35:28 +01:00
Tony Allevato
31d1b3a6d8 Index expressible-by-literal expressions.
When a value is initialized or coerced for a type that conforms to
one of the `ExpressibleBy*Literal` protocols (or
`ExpressibleByStringInterpolation`), this change records an implicit
call to the corresponding `init(...Literal:)` in the indexstore,
located at the beginning of the literal.
2022-08-27 20:06:13 -07:00
Rintaro Ishizaki
8bda524cd2 [SourceKit] Update for primary associated types
* InterfaceGen reports a primary associated type as a reference to the
  'associatedtype' declaration
* CursorInfo on a primary associated type returns information of the
  'associatedtype' declaration

rdar://93275458
2022-05-18 17:16:49 -07:00
Ben Barham
fec7a0b79b [IDE] Skip walking serialized internal top level decls in SemaAnnotator
`SemaAnnotator` always attempts to retrieve the location of the decl. This
requires generating the USR, which needs to resolve the type. But that's
invalid in the presence of `@_implementationOnly`.

This most commonly comes up during index while building as even though
it skip internal decls, by that point it's too late (`SemaAnnotator` has
already tried retrieving the location). Other uses of
`SourceEntityWalker` should be unaffected as they only run over the
current decls in the current module.

For now skip walking internal top level decls from serialized modules in
`SemaAnnotator`. Consider expanding this to most, if not all,
`ASTWalker` clients in the future - it's unlikely `SemaAnnotator` is the
only one with this problem.

Resolves rdar://91279771.
2022-04-15 17:08:47 -07:00
Ian Leitch
1ef1e2f829 [Index] Apply RelationContainedBy role to references contained by VarDecl.
This reverts commit abf6a30ba0.
2021-11-30 10:54:14 +01:00
Ben Barham
abf6a30ba0 Revert "[Index] Apply RelationContainedBy role to references contained by VarDecl." 2021-11-16 08:56:01 +10:00
Ian Leitch
887e71009a [Index] Apply RelationContainedBy role to references contained by VarDecl.
References associated with a `VarDecl` had no `RelationContainedBy` role, resulting in "orphaned" references. From the perspective of identifying unused code (in tools using the index, like [Periphery](https://github.com/peripheryapp/periphery)), this made it impossible to identify that a variable's type, initializer and custom attributes are associated with the variable.

Resolves: [SR-13766](https://bugs.swift.org/browse/SR-13766)
2021-11-10 09:43:24 +00:00
Hamish Knight
78d8d09b99 [IDE] Adopt ArgumentList 2021-09-01 18:40:26 +01:00
Ben Barham
15c7ddd33f [IDE] Skip synthesized curry thunks and walk their unwrapped expression
SemaAnnotator was walking into an autoclosure and then manually running
`passReference` on the unwrapped expression without walking it. Since
its synthesized anyway, skip walking the autoclosure entirely and walk
the unwrapped expression instead.

Fix `swift::ide::isBeingCalled` to look through `IdentityExpr`s and
`swift::ide::getBase` also not unwrapping curry thunks.

Resolves rdar://81312849
2021-07-31 16:05:30 +10:00
Alex Hoppen
39e92db1b1 Merge pull request #38049 from ahoppen/pr/keypath-completion
[CodeCompletion] Migrate key path completion to be solver based
2021-06-30 22:03:44 +02:00
Alex Hoppen
d64b8ecea6 [CodeCompletion] Migrate key path completion to be solver based
This commit essentially consistes of the following steps:
- Add a new code completion key path component that represents the code completion token inside a key path. Previously, the key path would have an invalid component at the end if it contained a code completion token.
- When type checking the key path, model the code completion token’s result type by a new type variable that is unrelated to the previous components (because the code completion token might resolve to anything).
- Since the code completion token is now properly modelled in the constraint system, we can use the solver based code completion implementation and inspect any solution determined by the constraint solver. The base type for code completion is now the result type of the key path component that preceeds the code completion component.

This resolves bugs where code completion was not working correctly if the key path’s type had a generic base or result type. It’s also nice to have moved another completion type over to the solver-based implementation.

Resolves rdar://78779234 [SR-14685] and rdar://78779335 [SR-14703]
2021-06-25 23:19:35 +02:00
Fredrik Wieczerkowski
b2fb9e4494 Fix name range of wildcard names in walkToDeclPre 2021-06-24 22:01:32 +02:00
Hamish Knight
e9ba6c7240 Allow SemaAnnotator to handle patterns
Add the necessary walking hooks, and fix
ReferenceCollector to use it.
2021-06-09 10:43:24 +01:00
Hamish Knight
46fa6e5721 [AST] Improve BinaryExpr
Abstract away the TupleExpr gunk and expose
`getLHS` and `getRHS` accessors. This is in
preparation for completely expunging the use
of TupleExpr as an argument list.
2021-05-19 14:48:01 +01:00
Hamish Knight
854a8bcb42 [IDE] Allow SourceEntityWalker to keep track of its ASTWalker
This will allow it to be queried for things like
the parent ASTNode in the current traversal.
2021-05-14 11:17:59 +01:00
Alex Hoppen
981b663851 [IDE] Ensure every walkToPre call is matched with a walkToPost call in SemaAnnotator
We had some unbalanced calls of `walkTo*Pre` and `walkTo*Post` in `SemaAnnotator`.

The main fix was to set `Cancelled` to `true` if traversal is being stopped in `walkToExprPre`.

While I was at it, I also
- Added some more checks, ensuring that no more `walkTo*` calls are issued after `Cancelled` has been set to `true`.
- Added some comments, describing the intended traversal behaviour.
- Inverted the return value of the `ReportParamList` lambda to be in line with the return value of the enclosing `walkToDeclPre`
- Moved `walkToExprPost` to be place right after `walkToExprPre`

Resolves rdar://64139829 [SR-12957]
2021-03-30 12:35:29 +02:00
Ben Barham
ab55c19b44 [IDE] Propagate the Stmt visit failure while walking TopLevelCodeDecls
`visitTopLevelCodeDecl` ignored the `Stmt` visit returning a nullptr.
This caused the `walkToDeclPost` to run for the `TopLevelCodeDecl` and
thus an imbalance in the `RangeResolver` pre and posts (since none of
the children would have their `walkTo*Post` called).

This was originally incorrectly fixed while assuming that the
`walkTo*Post` are called regardless of whether the children were visited
or not. Those changes have been reverted - fixing an imbalance in
`ExtDecls` in `SemaAnnotator`.

Added a test case for the `ExtDecls` imbalance which can occur while an
extension is being added.

Resolves rdar://74820040
2021-03-06 11:45:11 +10:00
Ben Barham
33bc38cdb8 [IDE] SourceEntityWalker should walk all explicit declarations
`SourceEntityWalker` had an unbalanced `walkToDeclPre` and
`walkToDeclPost`, ie. `walkToDeclPost` could be called even though
`walkToDeclPre` was not. Specifically, this would occur for both
`OperatorDecl` and `PrecedenceGroupDecl` declarations.

These could both be added to the `if` in `walkToDeclPost`, but this
seems fairly errorprone in general - especially as new decls are added.
Indeed, there's already declarations that are being skipped because they
aren't explicitly tested for in `walkToDeclPre`, ie.
`PatternBindingDecl`.

Instead of skipping if not explcitly handled, only skip running the
`SEWalker` walk methods if the declaration is implicit (and not a
constructor decl, see TODO). This should probably also always visit
children, with various decls changed to become implicit (eg.
TopLevelCodeDecl), but we can do that later - breaks too many tests for
now.

This change exposed a few parameter declarations that were missing their
implicit flag, as well as unbalanced walk methods in `RangeResolver`.
2021-02-11 10:34:07 +10:00
Slava Pestov
e675bee26c AST: Split off DependencyCollector.h from EvaluatorDependencies.h
Also remove some unnecessary #includes from DependencyCollector.h,
which necessitated adding #includes in various other files.
2020-12-23 00:00:25 -05:00
Brent Royal-Gordon
cff4ddf13a [NFC] Adopt new ImportPath types and terminology
# Conflicts:
#	lib/IDE/CodeCompletion.cpp
2020-09-10 19:07:49 -07:00
Ben Barham
b4fb7d019a [IDE] Skip visiting constructor references when the decl is unknown
Have left in an assert since it would be nice to get a test case for
when this actually occurs.

Resolves rdar://67412430
2020-08-25 14:15:03 +10:00
AG
05c33330c9 Merge pull request #33370 from bitjammer/acgarland/rdar-65258208-crash-invalid-loc-extracttext
[SymbolGraph] Check Loc validity before extracting text
2020-08-10 11:56:53 -07:00
Ashley Garland
635550cd83 [SymbolGraph] Check Loc validity before extracting text
Hit this crash while walking an `import protocol ...` declaration.
Check loc validity before calling `SourceManager::extractText`:
there is an assertion at the beginning of this function.

rdar://65258208
2020-08-07 13:53:22 -07:00
Nathan Hawes
ef6c374516 [Sema/Index] Resolve #keyPath components so they can be indexed
Unlike \keypath expressions, only the property components of #keypath
expressions were being resolved, so index wouldn't pick up references for their
qualifying types.

Also fixes a code completion bug where it was reporting members from the Swift
rather than ObjC side of bridged types.

Resolves rdar://problem/61573935
2020-08-05 15:27:52 -07:00
Rintaro Ishizaki
00e4a76ef0 Revert "[Sema/Index] Resolve #keyPath components so they get handled by indexing, semantic highlighting, etc." 2020-08-04 12:51:52 -07:00
Nathan Hawes
4c7cd16c3f Merge pull request #33245 from nathawes/index-key-paths
[Sema/Index] Resolve #keyPath components so they get handled by indexing, semantic highlighting, etc.
2020-08-03 10:28:48 -07:00
Nathan Hawes
1d78fe1211 [Sema/Index] Resolve #keyPath components so they can be indexed
Unlike \keypath expressions, only the property components of #keypath
expressions were being resolved, so index wouldn't pick up references for their
qualifying types.

Also fixes a code completion bug where it was reporting members from the Swift
rather than ObjC side of bridged types.

Resolves rdar://problem/61573935
2020-07-31 17:11:23 -07:00
Suyash Srijan
58a84ea4c1 [NFC] Cleanup some code to use 'swift::getParameterList(ValueDecl)' to fetch parameter lists (#32979) 2020-07-20 19:50:37 +01:00
Robert Widmann
fb8fdd9644 Replace resolveCustomAttrType with a Request 2020-06-10 19:31:41 -07:00
Bruno Rocha
286b6cbb8f Visit base first 2020-05-30 14:34:42 +02:00
Bruno Rocha
cd838963a0 Index objcdynamic 2020-05-29 23:44:42 +02:00
Nathan Hawes
0f8619b943 [IDE][SourceKit] Support escaped identifiers for the syntactic rename and related idents requests.
Resolves rdar://problem/46409010
Resolves rdar://problem/48256383
2020-04-30 13:08:23 -07:00
Nathan Hawes
bce68fa4e5 [IDE][Refactoring] Handle 'callAsFunction' specially in syntactic rename.
This change makes us treat it exactly as we do 'init'. We don't allow renaming the base name,
and don't fail if the basename doesn't match for calls.

Also:
  - explicit init calls/references like `MyType.init(42)` are now reported with
    'init' as a keywordBase range, rather than nothing.
  - cursor info no longer reports rename as available on init/callAsFunction
    calls without arguments, as there's nothing to rename in that case.
  - Improved detection of when a referenced function is a call (rather than
    reference) across syntactic rename, cursor-info, and indexing.

Resolves rdar://problem/60340429
2020-04-12 17:14:15 -07:00
Slava Pestov
4caa6d7003 IDE: Fix indexing for Sema-built curry thunks 2020-03-17 19:03:03 -04:00