aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/lib/Tooling/NodeIntrospection.cpp
blob: f01bb1cb9c3cab58a1bac0a88f72a57b4e696b6c (plain) (blame)
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
//===- NodeIntrospection.h -----------------------------------*- C++ -*----===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
//  This file contains the implementation of the NodeIntrospection.
//
//===----------------------------------------------------------------------===//

#include "clang/Tooling/NodeIntrospection.h"

#include "clang/AST/AST.h"
#include "llvm/Support/raw_ostream.h"

namespace clang {

namespace tooling {

void LocationCallFormatterCpp::print(const LocationCall &Call,
                                     llvm::raw_ostream &OS) {
  if (const LocationCall *On = Call.on()) {
    print(*On, OS);
    if (On->returnsPointer())
      OS << "->";
    else
      OS << '.';
  }

  OS << Call.name() << "()";
}

std::string LocationCallFormatterCpp::format(const LocationCall &Call) {
  std::string Result;
  llvm::raw_string_ostream OS(Result);
  print(Call, OS);
  OS.flush();
  return Result;
}

namespace internal {

static bool locationCallLessThan(const LocationCall *LHS,
                                 const LocationCall *RHS) {
  if (!LHS && !RHS)
    return false;
  if (LHS && !RHS)
    return true;
  if (!LHS && RHS)
    return false;
  auto compareResult = LHS->name().compare(RHS->name());
  if (compareResult < 0)
    return true;
  if (compareResult > 0)
    return false;
  return locationCallLessThan(LHS->on(), RHS->on());
}

bool RangeLessThan::operator()(
    std::pair<SourceRange, SharedLocationCall> const &LHS,
    std::pair<SourceRange, SharedLocationCall> const &RHS) const {
  if (LHS.first.getBegin() < RHS.first.getBegin())
    return true;
  else if (LHS.first.getBegin() != RHS.first.getBegin())
    return false;

  if (LHS.first.getEnd() < RHS.first.getEnd())
    return true;
  else if (LHS.first.getEnd() != RHS.first.getEnd())
    return false;

  return locationCallLessThan(LHS.second.get(), RHS.second.get());
}
bool RangeLessThan::operator()(
    std::pair<SourceLocation, SharedLocationCall> const &LHS,
    std::pair<SourceLocation, SharedLocationCall> const &RHS) const {
  if (LHS.first == RHS.first)
    return locationCallLessThan(LHS.second.get(), RHS.second.get());
  return LHS.first < RHS.first;
}
} // namespace internal

} // namespace tooling
} // namespace clang

#include "clang/Tooling/NodeIntrospection.inc"