File: ASTPrint.h

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,368 kB
  • sloc: cpp: 5,593,980; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (115 lines) | stat: -rw-r--r-- 4,061 bytes parent folder | download | duplicates (19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//===- unittests/AST/ASTPrint.h ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Helpers to simplify testing of printing of AST constructs provided in the/
// form of the source code.
//
//===----------------------------------------------------------------------===//

#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/SmallString.h"
#include "gtest/gtest.h"

namespace clang {

using PrintingPolicyAdjuster = llvm::function_ref<void(PrintingPolicy &Policy)>;

template <typename NodeType>
using NodePrinter =
    std::function<void(llvm::raw_ostream &Out, const ASTContext *Context,
                       const NodeType *Node,
                       PrintingPolicyAdjuster PolicyAdjuster)>;

template <typename NodeType>
using NodeFilter = std::function<bool(const NodeType *Node)>;

template <typename NodeType>
class PrintMatch : public ast_matchers::MatchFinder::MatchCallback {
  using PrinterT = NodePrinter<NodeType>;
  using FilterT = NodeFilter<NodeType>;

  SmallString<1024> Printed;
  unsigned NumFoundNodes;
  PrinterT Printer;
  FilterT Filter;
  PrintingPolicyAdjuster PolicyAdjuster;

public:
  PrintMatch(PrinterT Printer, PrintingPolicyAdjuster PolicyAdjuster,
             FilterT Filter)
      : NumFoundNodes(0), Printer(std::move(Printer)),
        Filter(std::move(Filter)), PolicyAdjuster(PolicyAdjuster) {}

  void run(const ast_matchers::MatchFinder::MatchResult &Result) override {
    const NodeType *N = Result.Nodes.getNodeAs<NodeType>("id");
    if (!N || !Filter(N))
      return;
    NumFoundNodes++;
    if (NumFoundNodes > 1)
      return;

    llvm::raw_svector_ostream Out(Printed);
    Printer(Out, Result.Context, N, PolicyAdjuster);
  }

  StringRef getPrinted() const { return Printed; }

  unsigned getNumFoundNodes() const { return NumFoundNodes; }
};

template <typename NodeType> bool NoNodeFilter(const NodeType *) {
  return true;
}

template <typename NodeType, typename Matcher>
::testing::AssertionResult
PrintedNodeMatches(StringRef Code, const std::vector<std::string> &Args,
                   const Matcher &NodeMatch, StringRef ExpectedPrinted,
                   StringRef FileName, NodePrinter<NodeType> Printer,
                   PrintingPolicyAdjuster PolicyAdjuster = nullptr,
                   bool AllowError = false,
                   // Would like to use a lambda for the default value, but that
                   // trips gcc 7 up.
                   NodeFilter<NodeType> Filter = &NoNodeFilter<NodeType>) {

  PrintMatch<NodeType> Callback(Printer, PolicyAdjuster, Filter);
  ast_matchers::MatchFinder Finder;
  Finder.addMatcher(NodeMatch, &Callback);
  std::unique_ptr<tooling::FrontendActionFactory> Factory(
      tooling::newFrontendActionFactory(&Finder));

  bool ToolResult;
  if (FileName.empty()) {
    ToolResult = tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args);
  } else {
    ToolResult =
        tooling::runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName);
  }
  if (!ToolResult && !AllowError)
    return testing::AssertionFailure()
           << "Parsing error in \"" << Code.str() << "\"";

  if (Callback.getNumFoundNodes() == 0)
    return testing::AssertionFailure() << "Matcher didn't find any nodes";

  if (Callback.getNumFoundNodes() > 1)
    return testing::AssertionFailure()
           << "Matcher should match only one node (found "
           << Callback.getNumFoundNodes() << ")";

  if (Callback.getPrinted() != ExpectedPrinted)
    return ::testing::AssertionFailure()
           << "Expected \"" << ExpectedPrinted.str() << "\", got \""
           << Callback.getPrinted().str() << "\"";

  return ::testing::AssertionSuccess();
}

} // namespace clang