37 Commits

Author SHA1 Message Date
Alex Hoppen
c6c40de73d [IDE] Pass a SourceRange instead of a CharSourceRange in SemaAnnotator::passReference
Most `SemaAnnotator`s don’t actually care about the char source range. Instead, they only care about the start location of the reference, which is also included in `SourceRange`. Computing a `CharSourceRange` from a `SourceRange` is kind of expensive because it needs to start a new lexer.

To avoid this overhead, pass `SourceRange` to `SemaAnnotator::passReference` and related functions and let the clients compute the `CharSourceRange` when needed.

This reduces the overhead of index-while-building by about 10%.
2025-06-04 18:01:47 +02:00
Doug Gregor
4c4a70f042 Remove unused shouldWalkInactiveConfigRegion 2024-09-16 16:30:57 -07:00
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
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
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
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
4197bc2325 [Async Refactoring] Fix crash on recursive AST walk
The async refactoring may perform recursive AST
walks in cases such as transforming the body of
a hoisted closure. Make sure this can be handled
by the logic tracking the ASTWalker in the
SourceEntityWalker, such that we don't crash when
later converting the call to a completion callback.

rdar://78693050
2021-05-31 21:47:31 +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
Varun Gandhi
4028222107 [NFC] Remove redundant includes for <string>. 2020-05-31 13:05:46 -07:00
Ben Langmuir
3eff694bad [index] Add implicit callAsFunction reference to index
* Reference is marked "explicit", which may be unexpected - the reason
is that the *call* is explicit, so we want to find it with e.g. rename,
or looking up callers, even though the identifier callAsFunction is
implicit. This matches the behaviour of initializers.

* The source location is the same as the base name (e.g. in `add3(5)`,
it would be at `add3`), which matches the behaviour of initializers.

rdar://problem/60327632
2020-03-12 15:04:52 -07:00
Ben Langmuir
6af24d083c [index] Fix dynamicMemberLookup subscript reference implicit role
When building the implicit subscript expression, set the "implicit" bit
correctly and pass it through in the indexer so that we get implicit
refernces to the subscript. This would be useful for e.g. searching for
all uses of the dynamic subscript.
2019-04-16 15:37:32 -07:00
Slava Pestov
a70a1f0d36 IDE: Fix bug in SourceEntityWalker subscript handling
We weren't passing the lvalue access kind to the subscript.
This was causing a test to fail with the next patch.
2017-11-13 22:19:25 -08:00
Slava Pestov
0715eaeaed AST: Move SourceEntityWalker to IDE 2017-11-13 22:10:41 -08:00
Ben Langmuir
4d9c9f6098 Move SourceEntityWalker into libAST NFC
It fits right in among our other AST walkers and lets us use it lower in
the stack than libIDE.
2016-04-01 14:14:06 -07:00
practicalswift
1339b5403b Consistent use of header comment format.
Correct format:
//===--- Name of file - Description ----------------------------*- Lang -*-===//
2016-01-04 13:26:31 +01:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Xi Ge
5ca83d97a5 [InterfacePrint] Enhance ASTPrinter to support type-specific interface printing.
When users try to print the interface of a specific type (most often through cursor
infor query of SourceKit), we should simplify the original decls by replacing
archetypes with instantiated types, hiding extension details, and omitting
unfulfilled extension requirements. So the users can get the straight-to-the-point
"type interface". This commit builds the testing infrastructure for this feature,
and implements the first trick that wraps extension contents into the interface body.

This commit also moves some generic testing support from SourceKit to Swift.

Swift SVN r32630
2015-10-12 19:14:58 +00:00
Denis Vnukov
19f761d526 Adding a separate method for visiting subscript references in SourceEntityWalker.
When SourceEntityWalker visits a subscript reference it sometimes needed to visit
both open and close brackets. It used to be implemented as two calls to a regular
visitDeclReference which confused the clients expecting one call per a reference, 
for example indexing was recording two references to a subscript.

We add a separate visitSubscriptReference to resolve this problem.



Swift SVN r31494
2015-08-26 16:33:15 +00:00
Denis Vnukov
9ea05141b7 Reverted: Allowing SourceEntityWalker to skip visiting subscript end location
Swift SVN r31456
2015-08-25 22:04:19 +00:00
Denis Vnukov
fa860f05cc Allowing SourceEntityWalker to skip visiting subscript end location
Swift SVN r31451
2015-08-25 20:38:55 +00:00
Xi Ge
57489ceb0f [SyntaxHighlighting] Give ASTWalker the option to walk into generic params of function decls.
Therefore, generic params can also benefit from syntax highlighting and semantic annotation
in SourceKit. rdar://20529255

Swift SVN r30220
2015-07-15 18:28:09 +00:00
Xi Ge
fedf76577b [ASTWalker] Allow client to specify whether SourceEntityWalker should walk into inactive regions.
Swift SVN r30003
2015-07-09 00:36:44 +00:00
Doug Gregor
b8995b0aa3 Transform the Module class into ModuleDecl.
Modules occupy a weird space in the AST now: they can be treated like
types (Swift.Int), which is captured by ModuleType. They can be
treated like values for disambiguation (Swift.print), which is
captured by ModuleExpr. And we jump through hoops in various places to
store "either a module or a decl".

Start cleaning this up by transforming Module into ModuleDecl, a
TypeDecl that's implicitly created to describe a module. Subsequent
changes will start folding away the special cases (ModuleExpr ->
DeclRefExpr, name lookup results stop having a separate Module case,
etc.).

Note that the Module -> ModuleDecl typedef is there to limit the
changes needed. Much of this patch is actually dealing with the fact
that Module used to have Ctx and Name public members that now need to
be accessed via getASTContext() and getName(), respectively.

Swift SVN r28284
2015-05-07 21:10:50 +00:00
Argyrios Kyrtzidis
1552772a15 [AST] Move ModuleEntity from libIDE to libAST so that the ASTPrinter can use it.
Swift SVN r23680
2014-12-04 05:52:16 +00:00
Argyrios Kyrtzidis
3175251bb1 [IDE] Handle annotation of clang submodule imports properly.
rdar://17780613

Swift SVN r20595
2014-07-26 20:58:19 +00:00
Argyrios Kyrtzidis
ccc23b60b0 [IDE] Report module references in type contexts.
Swift SVN r17688
2014-05-08 07:10:05 +00:00
Argyrios Kyrtzidis
17c3bae3cd [IDE] Enhance SourceEntityWalker to provide callbacks for the keyword arguments of
a call expression.

Swift SVN r17607
2014-05-07 06:10:03 +00:00
Jordan Rose
417b5d3982 Merge TranslationUnit into Module, and eliminate the term "translation unit".
This completes the FileUnit refactoring. A module consists of multiple
FileUnits, which provide decls from various file-like sources. I say
"file-like" because the Builtin module is implemented with a single
BuiltinUnit, and imported Clang modules are just a single FileUnit source
within a module.

Most modules, therefore, contain a single file unit; only the main module
will contain multiple source files (and eventually partial AST files).

The term "translation unit" has been scrubbed from the project. To refer
to the context of declarations outside of any other declarations, use
"top-level" or "module scope". To refer to a .swift file or its DeclContext,
use "source file". To refer to a single unit of compilation, use "module",
since the model is that an entire module will be compiled with a single
driver call. (It will still be possible to compile a single source file
through the direct-to-frontend interface, but only in the context of the
whole module.)

Swift SVN r10837
2013-12-05 01:51:15 +00:00
Argyrios Kyrtzidis
76faca0269 [IDE] Enhance SourceEntityWalker to report walking of expressions.
Swift SVN r9760
2013-10-29 17:06:38 +00:00
Argyrios Kyrtzidis
65fd2eed43 [IDE] Provide 'walk' method in SourceEntityWalker for SourceFiles.
Swift SVN r9136
2013-10-10 14:51:37 +00:00
Argyrios Kyrtzidis
efe3d94e6b [AST] Introduce DeclContext::walkContext(), used for ASTWalking a DeclContext.
Swift SVN r8776
2013-09-30 15:43:09 +00:00
Argyrios Kyrtzidis
f32167d166 [AST] Introduce a walk() method for TranslationUnit.
Swift SVN r8729
2013-09-27 17:24:35 +00:00
Argyrios Kyrtzidis
e68201fa66 [IDE] Replace ide::findSemanticSourceEntities() with a more general SourceEntityWalker abstract class.
Swift SVN r8699
2013-09-26 21:36:03 +00:00