119740652SStephen Kelly //===- NodeIntrospection.h -----------------------------------*- C++ -*----===//
219740652SStephen Kelly //
319740652SStephen Kelly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
419740652SStephen Kelly // See https://llvm.org/LICENSE.txt for license information.
519740652SStephen Kelly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
619740652SStephen Kelly //
719740652SStephen Kelly //===----------------------------------------------------------------------===//
819740652SStephen Kelly //
919740652SStephen Kelly //  This file contains the implementation of the NodeIntrospection.
1019740652SStephen Kelly //
1119740652SStephen Kelly //===----------------------------------------------------------------------===//
1219740652SStephen Kelly 
1319740652SStephen Kelly #include "clang/Tooling/NodeIntrospection.h"
1419740652SStephen Kelly 
1519740652SStephen Kelly #include "clang/AST/AST.h"
16542e7806SNathan James #include "llvm/Support/raw_ostream.h"
1719740652SStephen Kelly 
1819740652SStephen Kelly namespace clang {
1919740652SStephen Kelly 
2019740652SStephen Kelly namespace tooling {
2119740652SStephen Kelly 
22542e7806SNathan James void LocationCallFormatterCpp::print(const LocationCall &Call,
23542e7806SNathan James                                      llvm::raw_ostream &OS) {
24542e7806SNathan James   if (const LocationCall *On = Call.on()) {
25542e7806SNathan James     print(*On, OS);
26542e7806SNathan James     if (On->returnsPointer())
27542e7806SNathan James       OS << "->";
28542e7806SNathan James     else
29542e7806SNathan James       OS << '.';
3019740652SStephen Kelly   }
31542e7806SNathan James 
32542e7806SNathan James   OS << Call.name();
33542e7806SNathan James   if (Call.args().empty()) {
34542e7806SNathan James     OS << "()";
35542e7806SNathan James     return;
3619740652SStephen Kelly   }
37542e7806SNathan James   OS << '(' << Call.args().front();
38542e7806SNathan James   for (const std::string &Arg : Call.args().drop_front()) {
39542e7806SNathan James     OS << ", " << Arg;
40542e7806SNathan James   }
41542e7806SNathan James   OS << ')';
42542e7806SNathan James }
43542e7806SNathan James 
44542e7806SNathan James std::string LocationCallFormatterCpp::format(const LocationCall &Call) {
45542e7806SNathan James   std::string Result;
46542e7806SNathan James   llvm::raw_string_ostream OS(Result);
47542e7806SNathan James   print(Call, OS);
48542e7806SNathan James   OS.flush();
49542e7806SNathan James   return Result;
5019740652SStephen Kelly }
5119740652SStephen Kelly 
5219740652SStephen Kelly namespace internal {
5319740652SStephen Kelly bool RangeLessThan::operator()(
54b23abbeaSNathan James     std::pair<SourceRange, SharedLocationCall> const &LHS,
55b23abbeaSNathan James     std::pair<SourceRange, SharedLocationCall> const &RHS) const {
5619740652SStephen Kelly   if (!LHS.first.isValid() || !RHS.first.isValid())
5719740652SStephen Kelly     return false;
5819740652SStephen Kelly 
5919740652SStephen Kelly   if (LHS.first.getBegin() < RHS.first.getBegin())
6019740652SStephen Kelly     return true;
6119740652SStephen Kelly   else if (LHS.first.getBegin() != RHS.first.getBegin())
6219740652SStephen Kelly     return false;
6319740652SStephen Kelly 
6419740652SStephen Kelly   if (LHS.first.getEnd() < RHS.first.getEnd())
6519740652SStephen Kelly     return true;
6619740652SStephen Kelly   else if (LHS.first.getEnd() != RHS.first.getEnd())
6719740652SStephen Kelly     return false;
6819740652SStephen Kelly 
69*4f6d6984SStephen Kelly   return LocationCallFormatterCpp::format(*LHS.second) <
70*4f6d6984SStephen Kelly          LocationCallFormatterCpp::format(*RHS.second);
7119740652SStephen Kelly }
72b23abbeaSNathan James bool RangeLessThan::operator()(
73b23abbeaSNathan James     std::pair<SourceLocation, SharedLocationCall> const &LHS,
74b23abbeaSNathan James     std::pair<SourceLocation, SharedLocationCall> const &RHS) const {
75b23abbeaSNathan James   if (LHS.first == RHS.first)
76*4f6d6984SStephen Kelly     return LocationCallFormatterCpp::format(*LHS.second) <
77*4f6d6984SStephen Kelly            LocationCallFormatterCpp::format(*RHS.second);
78b23abbeaSNathan James   return LHS.first < RHS.first;
79b23abbeaSNathan James }
8019740652SStephen Kelly } // namespace internal
8119740652SStephen Kelly 
8219740652SStephen Kelly } // namespace tooling
8319740652SStephen Kelly } // namespace clang
8419740652SStephen Kelly 
8519740652SStephen Kelly #include "clang/Tooling/NodeIntrospection.inc"
86