aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Tooling/Syntax
AgeCommit message (Collapse)AuthorFilesLines
2025-09-12[clang] AST: remove DependentTemplateSpecializationType (#158109)Matheus Izvekov1-7/+0
A DependentTemplateSpecializationType (DTST) is basically just a TemplateSpecializationType (TST) with a hardcoded DependentTemplateName (DTN) as its TemplateName. This removes the DTST and replaces all uses of it with a TST, removing a lot of duplication in the implementation. Technically the hardcoded DTN is an optimization for a most common case, but the TST implementation is in better shape overall and with other optimizations, so this patch ends up being an overall performance positive: <img width="1465" height="38" alt="image" src="https://github.com/user-attachments/assets/084b0694-2839-427a-b664-eff400f780b5" /> A DTST also didn't allow a template name representing a DTN that was substituted, such as from an alias template, while the TST does allow it by the simple fact it can hold an arbitrary TemplateName, so this patch also increases the amount of sugar retained, while still being faster overall. Example (from included test case): ```C++ template<template<class> class TT> using T1 = TT<int>; template<class T> using T2 = T1<T::template X>; ``` Here we can now represent in the AST that `TT` was substituted for the dependent template name `T::template X`.
2025-09-02[Clang] [C2y] Implement N3355 ‘Named Loops’ (#152870)Sirraide1-4/+2
This implements support for [named loops](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm) for C2y. When parsing a `LabelStmt`, we create the `LabeDecl` early before we parse the substatement; this label is then passed down to `ParseWhileStatement()` and friends, which then store it in the loop’s (or switch statement’s) `Scope`; when we encounter a `break/continue` statement, we perform a lookup for the label (and error if it doesn’t exist), and then walk the scope stack and check if there is a scope whose preceding label is the target label, which identifies the jump target. The feature is only supported in C2y mode, though a cc1-only option exists for testing (`-fnamed-loops`), which is mostly intended to try and make sure that we don’t have to refactor this entire implementation when/if we start supporting it in C++. --------- Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
2025-08-09[clang] Improve nested name specifier AST representation (#147835)Matheus Izvekov1-86/+85
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-07-18[Clang][AST][NFC] Introduce `NamespaceBaseDecl` (#149123)Yanzuo Liu1-1/+0
Add `NamespaceBaseDecl` as common base class of `NamespaceDecl` and `NamespaceAliasDecl`. This simplifies `NestedNameSpecifier` a bit. Co-authored-by: Matheus Izvekov <mizvekov@gmail.com>
2025-05-31[Tooling] Remove unused includes (NFC) (#142257)Kazu Hirata4-17/+0
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-04-01[clang] improved preservation of template keyword (#133610)Matheus Izvekov1-2/+0
2024-06-07[clangd] Fix crash with null check for Token at Loc (#94528)Utkarsh Saxena1-3/+4
Fixes https://github.com/llvm/llvm-project/issues/94599
2024-05-14Reapply "[Clang] Unify interface for accessing template arguments as written ↵Krystian Stasiowski1-1/+2
for class/variable template specializations (#81642)" (#91393) Reapplies #81642, fixing the crash which occurs when running the lldb test suite.
2024-05-07Revert "[Clang] Unify interface for accessing template arguments as written ↵Adrian Prantl1-2/+1
for class/variable template specializations (#81642)" This reverts commit 7115ed0fff027b65fa76fdfae215ed1382ed1473. This commit broke several LLDB tests. https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/as-lldb-cmake/3480/
2024-05-07[Clang] Unify interface for accessing template arguments as written for ↵Krystian Stasiowski1-1/+2
class/variable template specializations (#81642) Our current method of storing the template arguments as written for `(Class/Var)Template(Partial)SpecializationDecl` suffers from a number of flaws: - We use `TypeSourceInfo` to store `TemplateArgumentLocs` for class template/variable template partial/explicit specializations. For variable template specializations, this is a rather unintuitive hack (as we store a non-type specialization as a type). Moreover, we don't ever *need* the type as written -- in almost all cases, we only want the template arguments (e.g. in tooling use-cases). - The template arguments as written are stored in a number of redundant data members. For example, `(Class/Var)TemplatePartialSpecialization` have their own `ArgsAsWritten` member that stores an `ASTTemplateArgumentListInfo` (the template arguments). `VarTemplateSpecializationDecl` has yet _another_ redundant member "`TemplateArgsInfo`" that also stores an `ASTTemplateArgumentListInfo`. This patch eliminates all `(Class/Var)Template(Partial)SpecializationDecl` members which store the template arguments as written, and turns the `ExplicitInfo` member into a `llvm::PointerUnion<const ASTTemplateArgumentListInfo*, ExplicitInstantiationInfo*>` (to avoid unnecessary allocations when the declaration isn't an explicit instantiation). The template arguments as written are now accessed via `getTemplateArgsWritten` in all cases. The "most breaking" change is to AST Matchers, insofar that `hasTypeLoc` will no longer match class template specializations (since they no longer store the type as written).
2024-01-18[clangd] Handle an expanded token range that ends in the `eof` token in ↵Nathan Ridge1-0/+6
TokenBuffer::spelledForExpanded() (#78092) Such ranges can legitimately arise in the case of invalid code, such as a declaration missing an ending brace. Fixes https://github.com/clangd/clangd/issues/1559
2023-09-08Reapply "[clang] NFCI: Adopt `SourceManager::getFileEntryRefForID()`"Jan Svoboda1-1/+1
This reapplies ddbcc10b9e26b18f6a70e23d0611b9da75ffa52f, except for a tiny part that was reverted separately: 65331da0032ab4253a4bc0ddcb2da67664bd86a9. That will be reapplied later on, since it turned out to be more involved. This commit is enabled by 5523fefb01c282c4cbcaf6314a9aaf658c6c145f and f0f548a65a215c450d956dbcedb03656449705b9, specifically the part that makes 'clang-tidy/checkers/misc/header-include-cycle.cpp' separator agnostic.
2023-09-08[clang][lex] Use preferred path separator in includer-relative lookupJan Svoboda1-2/+2
There is a long-standing FIXME in `HeaderSearch.cpp` to use the path separator preferred by the platform instead of forward slash. There was an attempt to fix that (1cf6c28a) which got reverted (cf385dc8). I couldn't find an explanation, but my guess is that some tests assuming forward slash started failing. This commit fixes tests with that assumption. This is intended to be NFC, but there are two exceptions to that: * Some diagnostic messages might now contain backslash instead of forward slash. * Arguments to the "-remap-file" option that use forward slash might stop kicking in. Separators between potential includer path and header name need to be replaced by backslash in that case.
2023-09-06Revert "[clang] NFCI: Adopt `SourceManager::getFileEntryRefForID()`"Jan Svoboda1-1/+1
This reverts commit ddbcc10b9e26b18f6a70e23d0611b9da75ffa52f. The 'clang-tidy/checkers/misc/header-include-cycle.cpp' test started failing on Windows: https://lab.llvm.org/buildbot/#/builders/216/builds/26855.
2023-09-06[clang] NFCI: Adopt `SourceManager::getFileEntryRefForID()`Jan Svoboda1-1/+1
This commit replaces some calls to the deprecated `FileEntry::getName()` with `FileEntryRef::getName()` by swapping current usages of `SourceManager::getFileEntryForID()` with `SourceManager::getFileEntryRefForID()`. This lowers the number of usages of the deprecated `FileEntry::getName()` from 95 to 50.
2023-07-03[clang][tooling] Fix early termination when there are nested expansionsKadir Cetinkaya1-57/+5
This also does some cleanups, I am happy to undo them (or send as separate patches): - Change the early exit to stop only once we hit an expansion inside the main file, to make sure we keep following the nested expansions. - Add more tests to cover all the cases mentioned in the implementation - Drop the adjustments for prev/next tokens. We do the final checks based on the expansion locations anyway, so any intermediate mapping was a no-op. Differential Revision: https://reviews.llvm.org/D154335
2023-04-28[NFC][clang] Fix static analyzer concerns about AUTO_CAUSES_COPYManna, Soumi1-1/+1
Reported by Coverity: AUTO_CAUSES_COPY Unnecessary object copies can affect performance 1. Inside "ODRHash.cpp" file, in clang::ODRHash::AddCXXRecordDecl(clang::CXXRecordDecl const *): Using the auto keyword without an & causes the copy of an object of type CXXBaseSpecifier. 2. Inside "Tokens.cpp" file, in clang::syntax::TokenBuffer::dumpForTests[abi:cxx11](): Using the auto keyword without an & causes the copy of an object of type DenseMapPair. 3. Inside "TargetID.cpp" file, in clang::getCanonicalTargetID[abi:cxx11](llvm::StringRef, llvm::StringMap<bool, llvm::MallocAllocator> const &): Using the auto keyword without an & causes the copy of an object of type pair. Reviewed By: tahonermann, aaron.ballman Differential Revision: https://reviews.llvm.org/D148639
2023-01-14[clang] Remove remaining uses of llvm::Optional (NFC)Kazu Hirata1-1/+0
This patch removes several "using" declarations and #include "llvm/ADT/Optional.h". This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14[clang] Use std::optional instead of llvm::Optional (NFC)Kazu Hirata1-1/+1
This patch replaces (llvm::|)Optional< with std::optional<. I'll post a separate patch to remove #include "llvm/ADT/Optional.h". This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14[clang] Add #include <optional> (NFC)Kazu Hirata1-0/+1
This patch adds #include <optional> to those files containing llvm::Optional<...> or Optional<...>. I'll post a separate patch to actually replace llvm::Optional with std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-09Move from llvm::makeArrayRef to ArrayRef deduction guides - clang/ partserge-sans-paille3-20/+20
This is a follow-up to https://reviews.llvm.org/D140896, split into several parts as it touches a lot of files. Differential Revision: https://reviews.llvm.org/D141139
2022-12-14[clang] Remove uses of ::testing::Matcher<const Optional<T> &>Fangrui Song1-2/+2
Change a few functions (getCheckTraversalKind, some clang/Tooling/ API, etc) from llvm::Optional to std::optional.
2022-12-08[clang] Don't including None.h (NFC)Kazu Hirata1-1/+0
These source files no longer use None, so they do not need to include None.h. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-05Remove unused #include "llvm/ADT/Optional.h"Fangrui Song1-1/+0
2022-12-03[clang] Use std::nullopt instead of None (NFC)Kazu Hirata1-7/+7
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-10-05[Syntax] Fix macro-arg handling in TokenBuffer::spelledForExpandedSam McCall1-63/+155
A few cases were not handled correctly. Notably: #define ID(X) X #define HIDE a ID(b) HIDE spelledForExpanded() would claim HIDE is an equivalent range of the 'b' it contains, despite the fact that HIDE also covers 'a'. While trying to fix this bug, I found findCommonRangeForMacroArgs hard to understand (both the implementation and how it's used in spelledForExpanded). It relies on details of the SourceLocation graph that are IMO fairly obscure. So I've added/revised quite a lot of comments and made some naming tweaks. Fixes https://github.com/clangd/clangd/issues/1289 Differential Revision: https://reviews.llvm.org/D134618
2022-08-01Fixed a number of typosGabriel Ravier1-1/+1
I went over the output of the following mess of a command: (ulimit -m 2000000; ulimit -v 2000000; git ls-files -z | parallel --xargs -0 cat | aspell list --mode=none --ignore-case | grep -E '^[A-Za-z][a-z]*$' | sort | uniq -c | sort -n | grep -vE '.{25}' | aspell pipe -W3 | grep : | cut -d' ' -f2 | less) and proceeded to spend a few days looking at it to find probable typos and fixed a few hundred of them in all of the llvm project (note, the ones I found are not anywhere near all of them, but it seems like a good start). Differential Revision: https://reviews.llvm.org/D130827
2022-07-15[syntax] Some #includes cleanup, NFC.Haojian Wu2-3/+2
2022-07-15[syntax] Introduce a TokenManager interface.Haojian Wu7-94/+114
TokenManager defines Token interfaces for the clang syntax-tree. This is the level of abstraction that the syntax-tree should use to operate on Tokens. It decouples the syntax-tree from a particular token implementation (TokenBuffer previously). This enables us to use a different underlying token implementation for the syntax Leaf node -- in clang pseudoparser, we want to produce a syntax-tree with its own pseudo::Token rather than syntax::Token. Differential Revision: https://reviews.llvm.org/D128411
2022-06-20Don't use Optional::hasValue (NFC)Kazu Hirata1-2/+2
2022-03-16Reapply [pseudo] Move pseudoparser from clang to clang-tools-extra"Sam McCall12-2159/+0
This reverts commit 049f4e4eab19c6e468e029232e94ca71245b0f56. The problem was a stray dependency in CLANG_TEST_DEPS which caused cmake to fail if clang-pseudo wasn't built. This is now removed.
2022-03-16Revert "[pseudo] Move pseudoparser from clang to clang-tools-extra"Sam McCall12-0/+2159
This reverts commit b97856c4cfe7efb13887d5691002a7aa38440924. Breaks a bunch of bots: https://lab.llvm.org/buildbot/#/builders/193/builds/8513
2022-03-16[pseudo] Move pseudoparser from clang to clang-tools-extraSam McCall12-2159/+0
This should make clearer that: - it's not part of clang proper - there's no expectation to update it along with clang (beyond green tests) - clang should not depend on it This is intended to be expose a library, so unlike other tools has a split between include/ and lib/. The main renames are: clang/lib/Tooling/Syntax/Pseudo/* => clang-tools-extra/pseudo/lib/* clang/include/clang/Tooling/Syntax/Pseudo/* => clang-tools-extra/pseudo/include/clang-pseudo/* clang/tools/clang/pseudo/* => clang-tools-extra/pseudo/tool/* clang/test/Syntax/* => clang-tools-extra/pseudo/test/* clang/unittests/Tooling/Syntax/Pseudo/* => clang-tools-extra/pseudo/unittests/* #include "clang/Tooling/Syntax/Pseudo/*" => #include "clang-pseudo/*" namespace clang::syntax::pseudo => namespace clang::pseudo check-clang => check-clang-pseudo clangToolingSyntaxPseudo => clangPseudo The clang-pseudo and ClangPseudoTests binaries are not renamed. See discussion around: https://discourse.llvm.org/t/rfc-a-c-pseudo-parser-for-tooling/59217/50 Differential Revision: https://reviews.llvm.org/D121233
2022-03-07[pseudo] Strip comments for TokenStream.Haojian Wu1-0/+11
Add a utility function to strip comments from a "raw" tokenstream. The derived stream will be fed to the GLR parser (for early testing). Differential Revision: https://reviews.llvm.org/D121092
2022-03-07[pseudo] empty parameter-declaration should be allowed in lambda declarator.Haojian Wu1-1/+1
This was an oversight, as we did a avoild-nullable modication to parameter-declaration-clause. Differential Revision: https://reviews.llvm.org/D121089
2022-03-07[pseudo] Rename {Preprocess,PPStructure} -> DirectiveMap. NFCSam McCall2-39/+42
More precisely describes what this file does. Per comments on https://reviews.llvm.org/D121092
2022-03-07[pseudo] Add readmeSam McCall1-0/+37
Differential Revision: https://reviews.llvm.org/D121108
2022-03-03[pseudo] Fix an out-of-bound access for LRTable::Actions.Haojian Wu1-0/+2
Without this patch, when End == Start, we access Actions[Actions.end()] though we return an empty result. This fixes an assertion failure in MSVC STL debug build.
2022-03-02[pseudo] fix some comment nits, NFC.Haojian Wu1-2/+2
2022-03-02[pseudo] Fix an out-of-bound error in LRTable::find.Haojian Wu1-6/+5
The linear scan should not escape the TargetedStates range. Differential Revision: https://reviews.llvm.org/D120723
2022-02-23[pseudo] fix an out-of-bound error in LRTable.Haojian Wu1-1/+1
Fix window debug build.
2022-02-23[Pseudo] Token/TokenStream, PP directive parser.Sam McCall4-0/+421
The TokenStream class is the representation of the source code that will be fed into the GLR parser. This patch allows a "raw" TokenStream to be built by reading source code. It also supports scanning a TokenStream to find the directive structure. Next steps (with placeholders in the code): heuristically choosing a path through #ifs, preprocessing the code by stripping directives and comments. These will produce a suitable stream to feed into the parser proper. Differential Revision: https://reviews.llvm.org/D119162
2022-02-23Silence some "not all control paths return a value" warnings; NFCAaron Ballman1-0/+1
2022-02-23[pseudo] Implement LRTable.Haojian Wu5-13/+287
This patch introduces a dense implementation of the LR parsing table, which is used by LR parsers. We build a SLR(1) parsing table from the LR(0) graph. Statistics of the LR parsing table on the C++ spec grammar: - number of states: 1449 - number of actions: 83069 - size of the table (bytes): 334928 Differential Revision: https://reviews.llvm.org/D118196
2022-02-09[NFC] Fix sign-compare warning in GrammarBNF thanks to int promotionErich Keane1-1/+1
2022-02-09[pseudo] Implement LRGraphHaojian Wu2-0/+232
LRGraph is the key component of the clang pseudo parser, it is a deterministic handle-finding finite-state machine, which is used to generated the LR parsing table. Separate from https://reviews.llvm.org/D118196. Differential Revision: https://reviews.llvm.org/D119172
2022-02-09[pseudo] Add first and follow set computation in Grammar.Haojian Wu1-0/+91
These will be used when building parsing table for LR parsers. Separate from https://reviews.llvm.org/D118196. Differential Revision: https://reviews.llvm.org/D118990
2022-02-04[syntax][pseudo] Introduce the C++ spec grammar.Haojian Wu2-1/+740
Add a dummy clang-pseudo tool (right now it accepts and parses the grammar file). Differential Revision: https://reviews.llvm.org/D115856
2022-02-04[pseudo] NFC, clangSyntaxPsuedo => clangToolingSyntaxPseudoHaojian Wu1-1/+1
To be consistent with existing name pattern.
2022-02-03[syntax][pseudo] Add Grammar for the clang pseudo-parserHaojian Wu4-0/+349
This patch introduces the Grammar class, which is a critial piece for constructing a tabled-based parser. As the first patch, the scope is limited to: - define base types (symbol, rules) of modeling the grammar - construct Grammar by parsing the BNF file (annotations are excluded for now) Differential Revision: https://reviews.llvm.org/D114790