aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Tooling/TransformerTest.cpp
AgeCommit message (Collapse)AuthorFilesLines
2023-09-08[clang][lex] Use preferred path separator in includer-relative lookupJan Svoboda1-9/+11
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-01-14[clang] Use std::optional instead of llvm::Optional (NFC)Kazu Hirata1-2/+2
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
2022-12-20[llvm] Make llvm::Any similar to std::anySebastian Neubauer1-1/+1
This facilitates replacing llvm::Any with std::any. - Deprecate any_isa in favor of using any_cast(Any*) and checking for nullptr because C++17 has no any_isa. - Remove the assert from any_cast(Any*), so it returns nullptr if the type is not correct. This aligns it with std::any_cast(any*). Use any_cast(Any*) throughout LLVM instead of checks with any_isa. This is the first part outlined in https://discourse.llvm.org/t/rfc-switching-from-llvm-any-to-std-any/67176 Differential Revision: https://reviews.llvm.org/D139973
2022-12-03[clang/unittests] Use std::nullopt instead of None (NFC)Kazu Hirata1-3/+3
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-07-23Use llvm::sort instead of std::sort where possibleDmitri Gribenko1-4/+4
llvm::sort is beneficial even when we use the iterator-based overload, since it can optionally shuffle the elements (to detect non-determinism). However llvm::sort is not usable everywhere, for example, in compiler-rt. Reviewed By: nhaehnle Differential Revision: https://reviews.llvm.org/D130406
2022-03-28[libTooling] Fix indentation. NFC.Eric Li1-47/+43
2022-03-21[libTooling] Generalize string explanation as templated metadataEric Li1-9/+77
Change RewriteRule from holding an `Explanation` to being able to generate arbitrary metadata. Where TransformerClangTidyCheck was interested in a string description for the diagnostic, other tools may be interested in richer metadata at a higher level of abstraction than at the edit level (which is currently available as ASTEdit::Metadata). Reviewed By: ymandel Differential Revision: https://reviews.llvm.org/D120360
2022-03-21Revert "[libTooling] Generalize string explanation as templated metadata"Yitzhak Mandelbaum1-77/+9
This reverts commit 18440547d3520b78c9ab929685309419fc1fbe95. Causing failures in some build modes. e.g. https://lab.llvm.org/buildbot/#/builders/217/builds/1886
2022-03-21[libTooling] Generalize string explanation as templated metadataEric Li1-9/+77
Change RewriteRule from holding an `Explanation` to being able to generate arbitrary metadata. Where TransformerClangTidyCheck was interested in a string description for the diagnostic, other tools may be interested in richer metadata at a higher level of abstraction than at the edit level (which is currently available as ASTEdit::Metadata). Reviewed By: ymandel Differential Revision: https://reviews.llvm.org/D120360
2022-02-15[libTooling] Change Tranformer's consumer to take multiple changesEric Li1-5/+49
Previously, Transformer would invoke the consumer once per file modified per match, in addition to any errors encountered. The consumer is not aware of which AtomicChanges come from any particular match. It is unclear which sets of edits may be related or whether an error invalidates any previously emitted changes. Modify the signature of the consumer to accept a set of changes. This keeps related changes (i.e. all edits from a single match) together, and clarifies that errors don't produce partial changes. Reviewed By: ymandel Differential Revision: https://reviews.llvm.org/D119745
2020-11-30[libTooling] Remove deprecated Clang Transformer declarationsYitzhak Mandelbaum1-6/+16
A number of declarations were leftover after the move from `clang::tooling` to `clang::transformer`. This patch removes those declarations and upgrades the handful of references to the deprecated declarations. Differential Revision: https://reviews.llvm.org/D92340
2020-11-20[libTooling] Update Transformer's `node` combinator to include the trailing ↵Yitzhak Mandelbaum1-5/+4
semicolon for decls. Currently, `node` only includes the semicolon for (some) statements. However, declarations have the same issue of (potentially) trailing semicolons, so `node` should behave the same for them. Differential Revision: https://reviews.llvm.org/D91872
2020-11-17[Transformer] Split ForStmt test into twoStephen Kelly1-7/+54
It is apparently not possible to have two rewrites in one gtest function because atomic changes in the test harness accumulate.
2020-11-17Comment out new test while I figure out what is wrong with itStephen Kelly1-3/+3
2020-11-17[AST] Update matchers to be traverse-awareStephen Kelly1-0/+26
Don't match Stmt or Decl nodes not spelled in the source when using TK_IgnoreUnlessSpelledInSource. This prevents accidental modification of source code at incorrect locations. Differential Revision: https://reviews.llvm.org/D90984
2020-11-17[AST] Ignore implicit nodes in IgnoreUnlessSpelledInSource modeStephen Kelly1-0/+180
Update the ASTNodeTraverser to dump only nodes spelled in source. There are only a few which need to be handled, but Decl nodes for which isImplicit() is true are handled together. Update the RAV instances used in ASTMatchFinder to ignore the nodes too. As with handling of template instantiations, it is necessary to allow the RAV to process the implicit nodes because they need to be visitable before the first traverse() matcher is encountered. An exception to this is in the MatchChildASTVisitor, because we sometimes wish to make a node matchable but make its children not-matchable. This is the case for defaulted CXXMethodDecls for example. Extend TransformerTests to illustrate the kinds of problems that can arise when performing source code rewriting due to matching implicit nodes. This change accounts for handling nodes not spelled in source when using direct matching of nodes, and when using the has() and hasDescendant() matchers. Other matchers such as cxxRecordDecl(hasMethod(cxxMethodDecl())) still succeed for compiler-generated methods for example after this change. Updating the implementations of hasMethod() and other matchers is for a follow-up patch. Differential Revision: https://reviews.llvm.org/D90982
2020-11-03Undo Revert "Ignore template instantiations if not in AsIs mode"Matt Morehouse1-0/+64
MaskRay already fixed the ASan bug.
2020-11-03Revert "Ignore template instantiations if not in AsIs mode"Matt Morehouse1-64/+0
This reverts commit 53df3beb624989ed32d87697d0c17601d7871465 due to check-asan failure on the buildbot.
2020-11-03[unittest][TrasnformerTest] Fix asan stack-use-after-returnFangrui Song1-2/+5
2020-11-02Ignore template instantiations if not in AsIs modeStephen Kelly1-0/+64
Summary: IgnoreUnlessSpelledInSource mode should ignore these because they are not written in the source. This matters for example when trying to replace types or values which are templated. The new test in TransformerTest.cpp in this commit demonstrates the problem. In existing matcher code, users can write `unless(isInTemplateInstantiation())` or `unless(isInstantiated())` (the user must know which to use). The point of the TK_IgnoreUnlessSpelledInSource mode is to allow the novice to avoid such details. This patch changes the IgnoreUnlessSpelledInSource mode to skip over implicit template instantiations. This patch does not change the TK_AsIs mode. Note: An obvious attempt at an alternative implementation would simply change the shouldVisitTemplateInstantiations() in ASTMatchFinder.cpp to return something conditional on the operational TraversalKind. That does not work because shouldVisitTemplateInstantiations() is called before a possible top-level traverse() matcher changes the operational TraversalKind. Reviewers: sammccall, aaron.ballman, gribozavr2, ymandel, klimek Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D80961
2020-10-22[libTooling] Add function to Transformer to create a no-op edit.Yitzhak Mandelbaum1-0/+8
This functionality is commonly needed in clang tidy checks (based on transformer) that only print warnings, without suggesting any edits. The no-op edit allows the user to associate a diagnostic message with a source location. Differential Revision: https://reviews.llvm.org/D89961
2020-09-03[libTooling] Provide overloads of `rewriteDescendants` that operate directly ↵Yitzhak Mandelbaum1-0/+83
on an AST node. The new overloads apply directly to a node, like the `clang::ast_matchers::match` functions, Rather than generating an `EditGenerator` combinator. Differential Revision: https://reviews.llvm.org/D87031
2020-09-02[libTooling] Restore defaults for matchers in makeRule.Yitzhak Mandelbaum1-31/+3
This patch restores the default traversal for Transformer's `makeRule` to `TK_AsIs`. The implicit mode has proven problematic. Differential Revision: https://reviews.llvm.org/D87048
2020-08-11[libTooling] Move RewriteRule include edits to ASTEdit granularity.Yitzhak Mandelbaum1-1/+67
Currently, changes to includes are applied to an entire rule. However, include changes may be specific to particular edits within a rule (for example, they may apply to one file but not another). Also, include changes may need to carry metadata, just like other changes. So, we make include changes first-class edits. Reviewed By: tdl-g Differential Revision: https://reviews.llvm.org/D85734
2020-07-24[libTooling] Add an `EditGenerator` that applies a rule throughout a bound node.Yitzhak Mandelbaum1-1/+117
The new combinator, `rewriteDescendants`, applies a rewrite rule to all descendants of a specified bound node. That rewrite rule can refer to nodes bound by the parent, both in the matcher and in the edits. Reviewed By: gribozavr2 Differential Revision: https://reviews.llvm.org/D84409
2020-07-24[libTooling] Add assorted `EditGenerator` combinators.Yitzhak Mandelbaum1-0/+120
Summary: This patch adds various combinators that help in constructing `EditGenerator`s: * `noEdits` * `ifBound`, specialized to `ASTEdit` * `flatten` and `flattenVector` which allow for easy construction from a set of sub edits. * `shrinkTo`, which generates edits to shrink a given range to another that it encloses. Reviewers: asoffer, gribozavr2 Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D84310
2020-07-21[libTooling] In Clang Transformer, change `Metadata` field to deferred ↵Andy Soffer1-3/+12
evaluation. `Metadata` is being changed from an `llvm::Any` to a `MatchConsumer<llvm::Any>` so that it's evaluation can be be dependent on on `MatchResult`s passed in. Reviewed By: ymandel, gribozavr2 Differential Revision: https://reviews.llvm.org/D83820
2020-07-20Revert "[libTooling] In Clang Transformer, change `Metadata` field to ↵Yitzhak Mandelbaum1-12/+3
deferred evalutaion" This reverts commit c0b8954ecba500e3d9609152295b74ccd7d89d62. The commit has broken various builds. Reverting while I investigate the cause.
2020-07-20[libTooling] In Clang Transformer, change `Metadata` field to deferred ↵Yitzhak Mandelbaum1-3/+12
evalutaion `Metadata` is being changed from an `llvm::Any` to a `MatchConsumer<llvm;:Any>`, so that it's evaluation can be be dependent on `MatchResult`s passed in. Reviewed By: ymandel, gribozavr2 Differential Revision: https://reviews.llvm.org/D83820
2020-06-30Add Metadata to Transformer toolingAndy Soffer1-0/+23
This change adds a Metadata field to ASTEdit, Edit, and AtomicChange so that edits can have associated metadata and that metadata can be constructed with Transformer-based RewriteRules. Metadata is ignored when applying edits to source, but other consumers of AtomicChange can use this metadata to direct how they want to consume each edit. Reviewed By: ymandel, gribozavr2 Differential Revision: https://reviews.llvm.org/D82226
2020-05-28[libTooling] Fix Transformer to work with ambient traversal kinds.Yitzhak Mandelbaum1-0/+53
Summary: `RewriteRule`'s `applyFirst` was brittle with respect to the default setting of the `TraversalKind`. This patch builds awareness of traversal kinds directly into rewrite rules so that they are insensitive to any changes in defaults. Reviewers: steveire, gribozavr Subscribers: hokein, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D80606
2020-05-26[libTooling] In Transformer, allow atomic changes to span multiple files.Yitzhak Mandelbaum1-0/+42
Summary: Currently, all changes returned by a single application of a rule must fit in one atomic change and therefore must apply to one file. However, there are patterns in which a single rule will want to modify multiple files; for example, a header and implementation to change a declaration and its definition. This patch relaxes Transformer, libTooling's interpreter of RewriteRules, to support multiple changes. Reviewers: gribozavr Subscribers: mgrang, jfb, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D80239
2020-01-29Fix clang unnittest build with GCC 5Benjamin Kramer1-1/+1
2020-01-28Make llvm::StringRef to std::string conversions explicit.Benjamin Kramer1-32/+39
This is how it should've been and brings it more in line with std::string_view. There should be no functional change here. This is mostly mechanical from a custom clang-tidy check, with a lot of manual fixups. It uncovers a lot of minor inefficiencies. This doesn't actually modify StringRef yet, I'll do that in a follow-up.
2019-11-11[libTooling] Further simplify `Stencil` type and introduce `MatchComputation`.Yitzhak Mandelbaum1-6/+9
Summary: This revision introduces a new interface `MatchComputation` which generalizes the `Stencil` interface and replaces the `std::function` interface of `MatchConsumer`. With this revision, `Stencil` (as an abstraction) becomes just one collection of implementations of `MatchComputation<std::string>`. Correspondingly, we remove the `Stencil` class entirely in favor of a simple type alias, deprecate `MatchConsumer` and change all functions that accepted `MatchConsumer<std::string>` to use `MatchComputation<std::string>` instead. Reviewers: gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D69802
2019-11-06[libTooling] Fix breakage from change #84922Yitzhak Mandelbaum1-1/+4
2019-11-06[libTooling] Small changes in Transformer API.Yitzhak Mandelbaum1-36/+37
Summary: * Rename `transformer::change` to `transformer::changeTo`, make `change` forward to `changeTo` and mark it deprecated. * Mark `transformer::text` and `transformer::selection` deprecated and migrate references to them in tests. Reviewers: ilya-biryukov Subscribers: gribozavr, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D69896
2019-10-16[libTooling] Fix r374962: add more Transformer forwarding decls.Yitzhak Mandelbaum1-1/+0
Summary: The move to a new, single namespace in r374962 left out some type definitions from the old namespace and resulted in one naming conflict (`text`). This revision adds aliases for those definitions and removes one of the `text` functions from the new namespace. Reviewers: alexfh Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D69036 llvm-svn: 375003
2019-10-16[libTooling] Put all Transformer declarations in a single namespace.Yitzhak Mandelbaum1-5/+6
Summary: This revision introduces a new namespace, `clang::transformer`, to hold the declarations for the Transformer library. Reviewers: gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D68876 llvm-svn: 374962
2019-10-10[libTooling] Move Transformer files to their own directory/library.Yitzhak Mandelbaum1-2/+2
Summary: The Transformer library has been growing inside of lib/Tooling/Refactoring. However, it's not really related to anything else in that directory. This revision moves all Transformer-related files into their own include & lib directories. A followup revision will (temporarily) add forwarding headers to help any users migrate their code to the new location. Reviewers: gribozavr Subscribers: mgorny, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D68637 llvm-svn: 374271
2019-09-27[libTooling] Transformer: refine `SourceLocation` specified as anchor of ↵Yitzhak Mandelbaum1-0/+51
changes. Summary: Every change triggered by a rewrite rule is anchored at a particular location in the source code. This patch refines how that location is chosen and defines it as an explicit function so it can be shared by other Transformer implementations. This patch was inspired by a bug found by a clang tidy, wherein two changes were anchored at the same location (the expansion loc of the macro) resulting in the discarding of the second change. Reviewers: gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66652 llvm-svn: 373093
2019-08-13[libTooling] In Transformer, generalize `applyFirst` to admit rules with ↵Yitzhak Mandelbaum1-46/+80
incompatible matchers. Summary: This patch removes an (artificial) limitation of `applyFirst`, which requires that all of the rules' matchers can be grouped together in a single `anyOf()`. This change generalizes the code to group the matchers into separate `anyOf`s based on compatibility. Correspondingly, `buildMatcher` is changed to `buildMatchers`, to allow for returning a set of matchers rather than just one. Reviewers: gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D65877 llvm-svn: 368681
2019-07-18[LibTooling] Relax Transformer to allow rewriting macro expansionsYitzhak Mandelbaum1-22/+102
Summary: Currently, Transformer rejects any changes to source locations inside macro expansions. This change relaxes that constraint to allow rewrites when the entirety of the expansion is replaced, since that can be mapped to replacing the entirety of the expansion range in the file source. This change makes Transformer consistent with the handling of edit ranges in `clang::edit::Commit` (which is used, for example, for applying `FixItHint`s from diagnostics). Reviewers: ilya-biryukov Subscribers: gribozavr, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64518 llvm-svn: 366473
2019-07-02[LibTooling] Extend `RewriteRule` with support for adding includes.Yitzhak Mandelbaum1-0/+36
Summary: This revision allows users to specify the insertion of an included directive (at the top of the file being rewritten) as part of a rewrite rule. These directives are bundled with `RewriteRule` cases, so that different cases can potentially result in different include actions. Reviewers: ilya-biryukov, gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D63892 llvm-svn: 364917
2019-06-06[LibTooling] Add insert/remove convenience functions for creating `ASTEdit`s.Yitzhak Mandelbaum1-0/+58
Summary: `change()` is an all purpose function; the revision adds simple shortcuts for the specific operations of inserting (before/after) or removing source. Reviewers: ilya-biryukov Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62621 llvm-svn: 362707
2019-05-24[LibTooling] Add Explanation parameter to `makeRule`.Yitzhak Mandelbaum1-2/+1
Summary: Conceptually, a single-case RewriteRule has a matcher, edit(s) and an (optional) explanation. `makeRule` previously only took the matcher and edit(s). This change adds (optional) support for the explanation. Reviewers: ilya-biryukov Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62390 llvm-svn: 361643
2019-05-22[LibTooling] Update Transformer to use RangeSelector instead of NodePart enum.Yitzhak Mandelbaum1-23/+22
Transformer provides an enum to indicate the range of source text to be edited. That support is now redundant with the new (and more general) RangeSelector library, so we remove the custom enum support in favor of supporting any RangeSelector. Reviewers: ilya-biryukov Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62149 llvm-svn: 361392
2019-05-17[LibTooling] Add support to Transformer for composing rules as an ordered ↵Yitzhak Mandelbaum1-2/+89
choice. This revision updates `RewriteRule` to support multiple subrules that are interpreted as an ordered-choice (apply the first one that matches). With this feature, users can write the rules that appear later in the list of subrules knowing that previous rules' patterns *have not matched*, freeing them from reasoning about those cases in the current pattern. Reviewers: ilya-biryukov Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D61335 llvm-svn: 361037
2019-04-30[LibTooling] Fix broken test after r359574.Yitzhak Mandelbaum1-3/+5
r359574 changed the way that failures are reported, which broke the test TransformerTest.NodePartNameDeclRefFailure which detects a faiure. llvm-svn: 359578