168 Commits

Author SHA1 Message Date
Matheus Izvekov
91cdd35008
[clang] Improve nested name specifier AST representation (#147835)
This is a major change on how we represent nested name qualifications in
the AST.

* The nested name specifier itself and how it's stored is changed. The
prefixes for types are handled within the type hierarchy, which makes
canonicalization for them super cheap, no memory allocation required.
Also translating a type into nested name specifier form becomes a no-op.
An identifier is stored as a DependentNameType. The nested name
specifier gains a lightweight handle class, to be used instead of
passing around pointers, which is similar to what is implemented for
TemplateName. There is still one free bit available, and this handle can
be used within a PointerUnion and PointerIntPair, which should keep
bit-packing aficionados happy.
* The ElaboratedType node is removed, all type nodes in which it could
previously apply to can now store the elaborated keyword and name
qualifier, tail allocating when present.
* TagTypes can now point to the exact declaration found when producing
these, as opposed to the previous situation of there only existing one
TagType per entity. This increases the amount of type sugar retained,
and can have several applications, for example in tracking module
ownership, and other tools which care about source file origins, such as
IWYU. These TagTypes are lazily allocated, in order to limit the
increase in AST size.

This patch offers a great performance benefit.

It greatly improves compilation time for
[stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for
`test_on2.cpp` in that project, which is the slowest compiling test,
this patch improves `-c` compilation time by about 7.2%, with the
`-fsyntax-only` improvement being at ~12%.

This has great results on compile-time-tracker as well:

![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831)

This patch also further enables other optimziations in the future, and
will reduce the performance impact of template specialization resugaring
when that lands.

It has some other miscelaneous drive-by fixes.

About the review: Yes the patch is huge, sorry about that. Part of the
reason is that I started by the nested name specifier part, before the
ElaboratedType part, but that had a huge performance downside, as
ElaboratedType is a big performance hog. I didn't have the steam to go
back and change the patch after the fact.

There is also a lot of internal API changes, and it made sense to remove
ElaboratedType in one go, versus removing it from one type at a time, as
that would present much more churn to the users. Also, the nested name
specifier having a different API avoids missing changes related to how
prefixes work now, which could make existing code compile but not work.

How to review: The important changes are all in
`clang/include/clang/AST` and `clang/lib/AST`, with also important
changes in `clang/lib/Sema/TreeTransform.h`.

The rest and bulk of the changes are mostly consequences of the changes
in API.

PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just
for easier to rebasing. I plan to rename it back after this lands.

Fixes #136624
Fixes https://github.com/llvm/llvm-project/issues/43179
Fixes https://github.com/llvm/llvm-project/issues/68670
Fixes https://github.com/llvm/llvm-project/issues/92757
2025-08-09 05:06:53 -03:00
Yanzuo Liu
4a9eaad9e1
[Clang][AST][NFC] Introduce NamespaceBaseDecl (#149123)
Add `NamespaceBaseDecl` as common base class of `NamespaceDecl` and
`NamespaceAliasDecl`. This simplifies `NestedNameSpecifier` a bit.

Co-authored-by: Matheus Izvekov <mizvekov@gmail.com>
2025-07-18 09:01:47 +08:00
Prajwal Nadig
0f48bafafc
[ExtractAPI] Include tilde in destructor name (#146001)
The subheading for a destructor contained only the identifier. The tilde
must also be included as it is necessary to differentiate the destructor
from any constructors present.

rdar://129587608
2025-07-07 09:14:47 -06:00
Prajwal Nadig
53102a395f
[ExtractAPI] Format pointer types correctly (#146182)
Pointer types in function signatures must place the asterisk before the
identifier without a space in between. This patch removes the space and
also ensures that pointers to pointers are formatted correctly.

rdar://131780418
rdar://154533037
2025-06-30 08:55:35 -06:00
Prajwal Nadig
23b66a68f1
[ExtractAPI] Include virtual keyword for methods (#145412)
This information was being left out of the symbol graph.

rdar://131780883
2025-06-23 17:10:43 -06:00
Prajwal Nadig
05b4bfe19e
[ExtractAPI] Include +/- symbols for ObjC methods (#145035)
ObjC methods include a +/- prefix to indicate if they are a class or
instance method. This information is valuable, and must be included in
the navigator generated by ExtractAPI.

rdar://150870936
2025-06-20 09:05:16 -06:00
Kazu Hirata
c01532177f
[clang] Remove unused includes (NFC) (#144285)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-06-15 21:00:36 -07:00
Serge Pavlov
5a1edf0f51
[NFC] Optimize file kind determination (#139492)
There are checks in clang codebase that determine the type of source
file, associated with a given location - specifically, if it is an
ordonary file or comes from sources like command-line options or a
built-in definitions. These checks often rely on calls to
`getPresumedLoc`, which is relatively expensive. In certain cases, these
checks are combined, leading to repeated calculations of the costly
function negatively affecting compile time.

This change tries to optimize such checks. It must fix compile time
regression introduced in
https://github.com/llvm/llvm-project/pull/137306/.

---------

Co-authored-by: cor3ntin <corentinjabot@gmail.com>
2025-05-12 15:22:44 +07:00
Matheus Izvekov
dc17429ae6
[clang] improved preservation of template keyword (#133610) 2025-04-01 17:15:18 -03:00
QuietMisdreavus
3386156b1e
[clang][ExtractAPI] fix a couple crashes when used via libclang (#132297)
This PR fixes two crashes in ExtractAPI that occur when decls are
requested via libclang:

- A null-dereference would sometimes happen in
`DeclarationFragmentsBuilder::getFragmentsForClassTemplateSpecialization`
when the template being processed was loaded indirectly via a typedef,
with parameters filled in. The first commit loads the template parameter
locations ahead of time to perform a null check before dereferencing.
- An assertion (or another null-dereference) was happening in
`CXXRecordDecl::bases` when processing a forward-declaration (i.e. a
record without a definition). The second commit guards the use of
`bases` in `ExtractAPIVisitorBase::getBases` by first checking that the
decl in question has a complete definition.

The added test `extract-api-cursor-cpp` adds tests for these two
scenarios to protect against the crash in the future.

Fixes rdar://140592475, fixes rdar://123430367
2025-03-26 17:46:21 -06:00
QuietMisdreavus
1be4a67454
[ExtractAPI] reorder the module names in extension symbol graph file names (#119925)
Resolves rdar://140298287

ExtractAPI's support for printing Objective-C category extensions from
other modules emits symbol graphs with an
`ExtendedModule@HostModule.symbols.json`. However, this is backwards
from existing symbol graph practices, causing issues when these symbol
graphs are consumed alongside symbol graphs generated with other tools
like Swift. This PR flips the naming scheme to be in line with existing
symbol graph tooling.
2024-12-16 13:36:19 -07:00
Aaron Ballman
af7c58b7ea
Remove support for RenderScript (#112916)
See
https://discourse.llvm.org/t/rfc-deprecate-and-eventually-remove-renderscript-support/81284
for the RFC
2024-10-28 12:48:42 -04:00
Daniel Grumberg
33fa40cc96
[clang][ExtractAPI] Generate subheading for typedef'd anonymous types (#110689)
When an anonymous type has a typedef we normally use the typedef's name
in places where we expect a named identifier in the symbol graph. This
extends this logic to apply to subheadings.

rdar://136690614
2024-10-02 11:14:27 +01:00
Jan Svoboda
b1aea98cfa
[clang] Make deprecations of some FileManager APIs formal (#110014)
Some `FileManager` APIs still return `{File,Directory}Entry` instead of
the preferred `{File,Directory}EntryRef`. These are documented to be
deprecated, but don't have the attribute that warns on their usage. This
PR marks them as such with `LLVM_DEPRECATED()` and replaces their usage
with the recommended counterparts. NFCI.
2024-09-25 10:36:44 -07:00
Youngsuk Kim
ac664697c5 [clang] Tidy uses of raw_string_ostream (NFC)
As specified in the docs,
1) raw_string_ostream is always unbuffered and
2) the underlying buffer may be used directly

( 65b13610a5226b84889b923bae884ba395ad084d for further reference )

* Don't call raw_string_ostream::flush(), which is essentially a no-op.
* Avoid unneeded calls to raw_string_ostream::str(), to avoid excess indirection.
2024-09-19 14:56:45 -05:00
Daniel Grumberg
cf1ad28169
[clang][ExtractAPI] Handle AttributedType fragments transparently (#107262)
rdar://131958623
2024-09-05 09:15:09 +01:00
Daniel Grumberg
86835d2d5a
[clang][ExtractAPI] Remove erroneous module name check in MacroCallbacks (#107059)
rdar://135044923
2024-09-03 16:21:35 +01:00
Daniel Grumberg
b9f4afa167
[clang][ExtractAPI] Fix iteration order of TopLevelRecords (#106411)
Fixes #106355
2024-08-29 10:02:01 +01:00
Daniel Grumberg
b1b24d7517
[clang][ExtractAPI] Fix quirks in interaction with submodules (#105868)
Extension SGFs require the module system to be enabled in order to discover which module defines the extended external type.
This patch ensures the following:
- Associate symbols with their top level module name, and that only top level modules are considered as modules for emitting extension SGFs.
- Ensure we don't drop macro definitions that came from a submodule. To this end look at all defined macros in `PPCalbacks::EndOfMainFile` instead of relying on `PPCallbacks::MacroDefined` being called to detect a macro definition.
2024-08-27 13:50:41 +01:00
Daniel Grumberg
8f4f3df3c0
Reenable anon structs (#104922)
Add back missing includes and revert revert "[clang][ExtractAPI] Stop
dropping fields of nested anonymous record types when they aren't
attached to variable declaration (#104600)"
2024-08-20 15:36:46 +01:00
Daniel Grumberg
b18b4547f1 Revert "[clang][ExtractAPI] Stop dropping fields of nested anonymous record types when they aren't attached to variable declaration (#104600)"
This reverts commit c60da1a271a6bb271e7703b2f7c71fbece67ab78.
2024-08-19 16:06:43 +01:00
Daniel Grumberg
c60da1a271
[clang][ExtractAPI] Stop dropping fields of nested anonymous record types when they aren't attached to variable declaration (#104600)
- Introduce primitives for removing records from `APISet` and managing
the record chain of `RecordContext`
- Detect nested anonymous record types and remove them from the `APISet`
after they have been fully traversed and transfer ownership of child
records to the parent context (if any)
2024-08-19 15:57:43 +01:00
Daniel Grumberg
57abd4e4ab
[clang][ExtractAPI] Emit environment component of target triple in SGF (#103273)
rdar://133533830
2024-08-15 16:23:31 +01:00
Daniel Grumberg
026d963cb0
[clang][ExtractAPI] Compute inherited availability information (#103040)
Additionally this computes availability information for all platforms
ahead of possibly introducing a flag to enable this behavior.

rdar://123513706
2024-08-15 14:19:49 +01:00
Daniel Grumberg
2c13194eab
[clang][ExtractAPI][NFC] Remove some nullptr dereference problems (#98914)
A places try to get a NamedDecl's name using getName when it isn't a
simple identifier, migrate these areas to getNameAsString.

rdar://125315602
2024-07-16 11:24:28 +01:00
Yuxuan Chen
f0785484c8
[clang][NFC] fix name lookup for llvm::json::Value in SymbolGraphSerializer (#94511)
This code uses namespaces `llvm` and `llvm::json`. However, we have both
`llvm::Value` and `llvm::json::Value`. Whenever any of the headers
declare or include `llvm::Value`, the lookup becomes ambiguous.

Fixing this by qualifying the `Value` type.
2024-06-06 11:32:19 -07:00
David Stone
aaa4ff88d6
[clang][Modules] Remove unnecessary includes of Module.h (#93417) 2024-06-03 14:49:04 +08:00
Daniel Grumberg
ab7e6b66fd
[clang][ExtractAPI] Ensure TemplateArgumentLocations are only accessed if available (#93205) 2024-05-24 16:32:25 +01:00
Matheus Izvekov
2bde13cda1
[clang] NFCI: use TemplateArgumentLoc for NTTP DefaultArgument (#92852)
This is an enabler for https://github.com/llvm/llvm-project/pull/92855

This allows an NTTP default argument to be set as an arbitrary
TemplateArgument, not just an expression.
This allows template parameter packs to have default arguments in the
AST, even though the language proper doesn't support the syntax for it.

This allows NTTP default arguments to be other kinds of arguments, like
packs, integral constants, and such.
2024-05-22 12:18:44 -03:00
Matheus Izvekov
e42b799bb2
[clang] NFCI: use TemplateArgumentLoc for type-param DefaultArgument (#92854)
This is an enabler for a future patch.

This allows an type-parameter default argument to be set as an arbitrary
TemplateArgument, not just a type.
This allows template parameter packs to have default arguments in the
AST, even though the language proper doesn't support the syntax for it.

This will be used in a later patch which synthesizes template parameter
lists with arbitrary default arguments taken from template
specializations.

There are a few places we used SubsType, because we only had a type, now
we use SubstTemplateArgument.
SubstTemplateArgument was missing arguments for setting Instantiation
location and entity names.
Adding those is needed so we don't regress in diagnostics.
2024-05-21 20:27:50 -03:00
Daniel Grumberg
50b2bd4a25
[clang][ExtractAPI] Remove symbols defined in categories to external types unless requested (#92522)
rdar://128259890
2024-05-20 08:59:02 +01:00
Daniel Grumberg
7a67479392
[clang][ExtractAPI] Correctly generate declaration fragments for non-type template parameters (#91958)
Previously we only generated declaration fragments for template type
parameters/arguments, this adds supports for most other possible
template parameters/arguments.

rdar://127732598
2024-05-17 10:33:31 +01:00
Daniel Grumberg
61d4ca8722
[clang][ExtractAPI] Distinguish between record kind for display and for RTTI (#91466)
rdar://127732562
2024-05-13 10:37:09 +01:00
Daniel Grumberg
2bcbe40f8a
[clang][ExtractAPI] Fix handling of anonymous TagDecls (#87772)
This changes the handling of anonymous TagDecls to the following rules:
- If the TagDecl is embedded in the declaration for some VarDecl (this
is the only possibility for RecordDecls), then pretend the child decls
belong to the VarDecl
- If it's an EnumDecl proceed as we did previously, i.e., embed it in
the enclosing DeclContext.

Additionally this fixes a few issues with declaration fragments not
consistently including "{ ... }" for anonymous TagDecls. To make testing
these additions easier this patch fixes some text declaration fragments
merging issues and updates tests accordingly.

rdar://121436298
2024-04-24 13:53:29 +01:00
Daniel Grumberg
05c1447b3e
[clang][ExtractAPI] Serialize platform specific unavailable attribute in symbol graphs (#89277)
rdar://125622225
2024-04-23 09:00:08 +01:00
Daniel Grumberg
e05c1b46d0
Reenable external categories (#87357)
Reenables b31414bf4f9898f7817a9fcf8a91f62ec26f3eaf.

Also adds a new warning for missing `--symbol-graph-dir` arg when
`--emit-extension-symbol-graphs` is provided. This also reverts the
commit that removed.
2024-04-03 10:18:05 +01:00
Erick Velez
2b6c038e1f
[clang][ExtractAPI] improve template argument name deduction (#77716)
The names of template arguments in partial specializations or parameters
used as types might be mangled according to index and depth. Instead of
looping through parameter lists to find matches like we do now, they can
be deduced via their QualTypes or as written from the AST.
2024-04-02 08:42:13 -07:00
Daniel Grumberg
209a1e8dfd Revert "[clang][ExtractAPI] Add ability to create multiple symbol graphs (#86676)"
This failed the test suite due to missing DiagGroup for a new warning.

This reverts commit b31414bf4f9898f7817a9fcf8a91f62ec26f3eaf.
2024-04-02 15:34:52 +01:00
Daniel Grumberg
b31414bf4f
[clang][ExtractAPI] Add ability to create multiple symbol graphs (#86676)
This extends ExtractAPI to take into account symbols defined in categories to types defined in an external module.
This introduces 2 new command line flags, `--symbol-graph-dir=DIR` and `--emit-extension-symbol-graphs`, when used together this generates additional symbol graph files at `DIR/ExtendedModule@ProductName.symbols.json` for each external module that is extended in this way.

Additionally this makes some cleanups to tests to make them more resilient and cleans up the `APISet` data structure.
2024-04-02 15:03:46 +01:00
Nathan Lanza
e66b670f3b
[CIR][Basic][NFC] Add the CIR language to the Language enum
Add the CIR language to the Language enum and the standard usages of it.

commit-id:fd12b2c2

Reviewers: bcardosolopes, AaronBallman, erichkeane

Reviewed By: AaronBallman, bcardosolopes

Pull Request: https://github.com/llvm/llvm-project/pull/86072
2024-03-21 19:53:48 -04:00
Cyndy Ishida
4c6043de0b
[clang][InstallAPI] Add input file support to library (#81701)
This patch adds support for expected InstallAPI inputs. InstallAPI
accepts a well defined filelist of headers and how those headers
represent a single library.

InstallAPI captures header files to determine linkable symbols to then
compare against what was compiled in a binary dylib and generate TBD
files.
2024-02-20 09:39:00 -08:00
Cyndy Ishida
e606dc1daf
[clang] Move AvailabilityInfo into AST library (#81897)
Previously this class was only used by ExtractAPI, but it will soon also
be needed by InstallAPI. This patch should not change availability
behavior but just centralizes the information next to what already is
captured about availability for AST traversal.
2024-02-15 17:14:54 -08:00
Kazu Hirata
26648daeb2 [ExtractAPI] Use StringRef::starts_with (NFC) 2024-01-28 10:47:05 -08:00
Daniel Grumberg
c5532124dc
[clang][ExtractAPI] Ensure typedef to pointer types are preserved (#78584)
When generating declaration fragments for types that use typedefs to
pointer types ensure that we keep the user-defined typedef form instead
of desugaring the typedef.

rdar://102137655
2024-01-22 15:41:29 +00:00
Daniel Grumberg
69fedaf830
[clang][ExtractAPI] Add support C unions in non C++ parsing mode (#77451)
Ensure that we generate correct symbol kinds and declaration fragments
for unions in C and Objective-C parsing modes.

rdar://120544091
2024-01-22 15:32:57 +00:00
Sofía Rodríguez
3d4128f7ff
[clang][ExtractAPI] Record availability information only for the target platform (#76823)
Currently, ExtractAPI provides availability information for all
platforms within a given domain. With this change, we narrow down the
output to include availability details only for the specified target
platform, so users can generate the symbol graph with only the
availability information they need, omitting information of the other
platforms.

This change reverts the functionality introduced in
[`57c9780`](https://github.com/llvm/llvm-project/commit/57c9780).

rdar://120419037
2024-01-19 10:56:53 +00:00
Kazu Hirata
f3dcc2351c
[clang] Use StringRef::{starts,ends}_with (NFC) (#75149)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-13 08:54:13 -08:00
Daniel Grumberg
789a5bbb7d
[clang][ExtractAPI] Allow serialization for ObjC++ headers (#74733)
rdar://79874441
2023-12-07 17:30:02 +00:00
Daniel Grumberg
14e991740b
[clang][ExtractAPI] Ensure LocationFileChecker doesn't try to traverse VFS when determining file path (#74071)
As part of https://reviews.llvm.org/D154130 the logic of
LocationFileChecker changed slightly to try and get the absolute
external file path instead of the name as requested when the file was
openened which would be before VFS mappings in our usage. Ensure that we
only check against the name as requested instead of trying to generate
the external canonical file path.

rdar://115195433
2023-12-01 15:54:36 +00:00
Daniel Grumberg
6b89fab897
[clang][ExtractAPI] Add support for blocks in declaration fragments (#73369)
Ensure that block types get represented correctly in declaration
fragments, as block parameter names are important for documentation
clients we need a separate system from getFragmentsForType in order to
have access to full ParmVarDecls for the parameters.

rdar://118257401
2023-11-28 12:55:27 +00:00