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 
print(const LocationCall & Call,llvm::raw_ostream & OS)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 
32ebc6608fSStephen Kelly   OS << Call.name() << "()";
33542e7806SNathan James }
34542e7806SNathan James 
format(const LocationCall & Call)35542e7806SNathan James std::string LocationCallFormatterCpp::format(const LocationCall &Call) {
36542e7806SNathan James   std::string Result;
37542e7806SNathan James   llvm::raw_string_ostream OS(Result);
38542e7806SNathan James   print(Call, OS);
39542e7806SNathan James   OS.flush();
40542e7806SNathan James   return Result;
4119740652SStephen Kelly }
4219740652SStephen Kelly 
4319740652SStephen Kelly namespace internal {
44*35918bcbSStephen Kelly 
locationCallLessThan(const LocationCall * LHS,const LocationCall * RHS)45*35918bcbSStephen Kelly static bool locationCallLessThan(const LocationCall *LHS,
46*35918bcbSStephen Kelly                                  const LocationCall *RHS) {
47*35918bcbSStephen Kelly   if (!LHS && !RHS)
48*35918bcbSStephen Kelly     return false;
49*35918bcbSStephen Kelly   if (LHS && !RHS)
50*35918bcbSStephen Kelly     return true;
51*35918bcbSStephen Kelly   if (!LHS && RHS)
52*35918bcbSStephen Kelly     return false;
53*35918bcbSStephen Kelly   auto compareResult = LHS->name().compare(RHS->name());
54*35918bcbSStephen Kelly   if (compareResult < 0)
55*35918bcbSStephen Kelly     return true;
56*35918bcbSStephen Kelly   if (compareResult > 0)
57*35918bcbSStephen Kelly     return false;
58*35918bcbSStephen Kelly   return locationCallLessThan(LHS->on(), RHS->on());
59*35918bcbSStephen Kelly }
60*35918bcbSStephen Kelly 
operator ()(std::pair<SourceRange,SharedLocationCall> const & LHS,std::pair<SourceRange,SharedLocationCall> const & RHS) const6119740652SStephen Kelly bool RangeLessThan::operator()(
62b23abbeaSNathan James     std::pair<SourceRange, SharedLocationCall> const &LHS,
63b23abbeaSNathan James     std::pair<SourceRange, SharedLocationCall> const &RHS) const {
6419740652SStephen Kelly   if (LHS.first.getBegin() < RHS.first.getBegin())
6519740652SStephen Kelly     return true;
6619740652SStephen Kelly   else if (LHS.first.getBegin() != RHS.first.getBegin())
6719740652SStephen Kelly     return false;
6819740652SStephen Kelly 
6919740652SStephen Kelly   if (LHS.first.getEnd() < RHS.first.getEnd())
7019740652SStephen Kelly     return true;
7119740652SStephen Kelly   else if (LHS.first.getEnd() != RHS.first.getEnd())
7219740652SStephen Kelly     return false;
7319740652SStephen Kelly 
74*35918bcbSStephen Kelly   return locationCallLessThan(LHS.second.get(), RHS.second.get());
7519740652SStephen Kelly }
operator ()(std::pair<SourceLocation,SharedLocationCall> const & LHS,std::pair<SourceLocation,SharedLocationCall> const & RHS) const76b23abbeaSNathan James bool RangeLessThan::operator()(
77b23abbeaSNathan James     std::pair<SourceLocation, SharedLocationCall> const &LHS,
78b23abbeaSNathan James     std::pair<SourceLocation, SharedLocationCall> const &RHS) const {
79b23abbeaSNathan James   if (LHS.first == RHS.first)
80*35918bcbSStephen Kelly     return locationCallLessThan(LHS.second.get(), RHS.second.get());
81b23abbeaSNathan James   return LHS.first < RHS.first;
82b23abbeaSNathan James }
8319740652SStephen Kelly } // namespace internal
8419740652SStephen Kelly 
8519740652SStephen Kelly } // namespace tooling
8619740652SStephen Kelly } // namespace clang
8719740652SStephen Kelly 
8819740652SStephen Kelly #include "clang/Tooling/NodeIntrospection.inc"
89