blob: 29f0a8572f07fa45e335451f988df5055cf8b315 [file] [log] [blame]
Luca Versari99fddff2022-05-25 10:22:32 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_TEMPLATE_PLACEHOLDER_SUPPORT_H_
6#define DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_TEMPLATE_PLACEHOLDER_SUPPORT_H_
7
8#include <functional>
9#include <string>
10
11#include "clang/AST/Decl.h"
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/Support/Error.h"
14
15namespace clang {
16namespace tidy {
17namespace lifetimes {
18
19struct GeneratedCode {
20 std::string filename;
21 std::string code;
22};
23
24// Generates a source code that includes the original code for `tu`
25// and also has explicit template instantiation code with placeholder
26// classes for the templates in `templates`.
27// For example, if the main file for the `tu` has the filename
28// "original-file.cc" and looks like the following:
29//
30// template <typename T>
31// T* target(T* t) {
32// return t;
33// }
34//
35// This will generate and return the code like the following
36// (actual generated placeholder classnames will be more cryptic than `T0`):
37//
38// #include "original-file.cc"
39// struct T0 {};
40// template T0* target<T0>(T0* t);
41//
42llvm::Expected<GeneratedCode> GenerateTemplateInstantiationCode(
43 const clang::TranslationUnitDecl* tu,
44 const llvm::DenseMap<clang::FunctionTemplateDecl*,
45 const clang::FunctionDecl*>& templates);
46
47// Runs the given `operation` on the `code` with `filename`. The `code` is
48// turned into a memory-backed file on a memory filesystem overlaid on top
49// of the original filesystem that's being used by `original_context`.
50void RunToolOnCodeWithOverlay(
51 clang::ASTContext& original_context, const std::string& filename,
52 const std::string& code,
53 const std::function<void(clang::ASTContext&)> operation);
54
55} // namespace lifetimes
56} // namespace tidy
57} // namespace clang
58
59#endif // DEVTOOLS_RUST_CC_INTEROP_LIFETIME_ANALYSIS_TEMPLATE_PLACEHOLDER_SUPPORT_H_