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 }
34 
35 std::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 
43 namespace internal {
44 bool RangeLessThan::operator()(
45     std::pair<SourceRange, SharedLocationCall> const &LHS,
46     std::pair<SourceRange, SharedLocationCall> const &RHS) const {
47   if (LHS.first.getBegin() < RHS.first.getBegin())
48     return true;
49   else if (LHS.first.getBegin() != RHS.first.getBegin())
50     return false;
51 
52   if (LHS.first.getEnd() < RHS.first.getEnd())
53     return true;
54   else if (LHS.first.getEnd() != RHS.first.getEnd())
55     return false;
56 
57   return LocationCallFormatterCpp::format(*LHS.second) <
58          LocationCallFormatterCpp::format(*RHS.second);
59 }
60 bool RangeLessThan::operator()(
61     std::pair<SourceLocation, SharedLocationCall> const &LHS,
62     std::pair<SourceLocation, SharedLocationCall> const &RHS) const {
63   if (LHS.first == RHS.first)
64     return LocationCallFormatterCpp::format(*LHS.second) <
65            LocationCallFormatterCpp::format(*RHS.second);
66   return LHS.first < RHS.first;
67 }
68 } // namespace internal
69 
70 } // namespace tooling
71 } // namespace clang
72 
73 #include "clang/Tooling/NodeIntrospection.inc"
74