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 
18 namespace clang {
19 
20 namespace tooling {
21 
22 void 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   if (Call.args().empty()) {
34     OS << "()";
35     return;
36   }
37   OS << '(' << Call.args().front();
38   for (const std::string &Arg : Call.args().drop_front()) {
39     OS << ", " << Arg;
40   }
41   OS << ')';
42 }
43 
44 std::string LocationCallFormatterCpp::format(const LocationCall &Call) {
45   std::string Result;
46   llvm::raw_string_ostream OS(Result);
47   print(Call, OS);
48   OS.flush();
49   return Result;
50 }
51 
52 namespace internal {
53 bool RangeLessThan::operator()(
54     std::pair<SourceRange, SharedLocationCall> const &LHS,
55     std::pair<SourceRange, SharedLocationCall> const &RHS) const {
56   if (!LHS.first.isValid() || !RHS.first.isValid())
57     return false;
58 
59   if (LHS.first.getBegin() < RHS.first.getBegin())
60     return true;
61   else if (LHS.first.getBegin() != RHS.first.getBegin())
62     return false;
63 
64   if (LHS.first.getEnd() < RHS.first.getEnd())
65     return true;
66   else if (LHS.first.getEnd() != RHS.first.getEnd())
67     return false;
68 
69   return LocationCallFormatterCpp::format(*LHS.second) <
70          LocationCallFormatterCpp::format(*RHS.second);
71 }
72 bool RangeLessThan::operator()(
73     std::pair<SourceLocation, SharedLocationCall> const &LHS,
74     std::pair<SourceLocation, SharedLocationCall> const &RHS) const {
75   if (LHS.first == RHS.first)
76     return LocationCallFormatterCpp::format(*LHS.second) <
77            LocationCallFormatterCpp::format(*RHS.second);
78   return LHS.first < RHS.first;
79 }
80 } // namespace internal
81 
82 } // namespace tooling
83 } // namespace clang
84 
85 #include "clang/Tooling/NodeIntrospection.inc"
86