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