1//===- NodeIntrospection.h -----------------------------------*- C++ -*----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//  This file contains the implementation of the NodeIntrospection.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Tooling/NodeIntrospection.h"
14
15#include "clang/AST/AST.h"
16#include "llvm/Support/raw_ostream.h"
17
18namespace clang {
19
20namespace tooling {
21
22void LocationCallFormatterCpp::print(const LocationCall &Call,
23                                     llvm::raw_ostream &OS) {
24  if (const LocationCall *On = Call.on()) {
25    print(*On, OS);
26    if (On->returnsPointer())
27      OS << "->";
28    else
29      OS << '.';
30  }
31
32  OS << Call.name() << "()";
33}
34
35std::string LocationCallFormatterCpp::format(const LocationCall &Call) {
36  std::string Result;
37  llvm::raw_string_ostream OS(Result);
38  print(Call, OS);
39  OS.flush();
40  return Result;
41}
42
43namespace internal {
44
45static bool locationCallLessThan(const LocationCall *LHS,
46                                 const LocationCall *RHS) {
47  if (!LHS && !RHS)
48    return false;
49  if (LHS && !RHS)
50    return true;
51  if (!LHS && RHS)
52    return false;
53  auto compareResult = LHS->name().compare(RHS->name());
54  if (compareResult < 0)
55    return true;
56  if (compareResult > 0)
57    return false;
58  return locationCallLessThan(LHS->on(), RHS->on());
59}
60
61bool RangeLessThan::operator()(
62    std::pair<SourceRange, SharedLocationCall> const &LHS,
63    std::pair<SourceRange, SharedLocationCall> const &RHS) const {
64  if (LHS.first.getBegin() < RHS.first.getBegin())
65    return true;
66  else if (LHS.first.getBegin() != RHS.first.getBegin())
67    return false;
68
69  if (LHS.first.getEnd() < RHS.first.getEnd())
70    return true;
71  else if (LHS.first.getEnd() != RHS.first.getEnd())
72    return false;
73
74  return locationCallLessThan(LHS.second.get(), RHS.second.get());
75}
76bool RangeLessThan::operator()(
77    std::pair<SourceLocation, SharedLocationCall> const &LHS,
78    std::pair<SourceLocation, SharedLocationCall> const &RHS) const {
79  if (LHS.first == RHS.first)
80    return locationCallLessThan(LHS.second.get(), RHS.second.get());
81  return LHS.first < RHS.first;
82}
83} // namespace internal
84
85} // namespace tooling
86} // namespace clang
87
88#include "clang/Tooling/NodeIntrospection.inc"
89