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 
32*ebc6608fSStephen Kelly   OS << Call.name() << "()";
33542e7806SNathan James }
34542e7806SNathan James 
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 {
4419740652SStephen Kelly bool RangeLessThan::operator()(
45b23abbeaSNathan James     std::pair<SourceRange, SharedLocationCall> const &LHS,
46b23abbeaSNathan James     std::pair<SourceRange, SharedLocationCall> const &RHS) const {
4719740652SStephen Kelly   if (!LHS.first.isValid() || !RHS.first.isValid())
4819740652SStephen Kelly     return false;
4919740652SStephen Kelly 
5019740652SStephen Kelly   if (LHS.first.getBegin() < RHS.first.getBegin())
5119740652SStephen Kelly     return true;
5219740652SStephen Kelly   else if (LHS.first.getBegin() != RHS.first.getBegin())
5319740652SStephen Kelly     return false;
5419740652SStephen Kelly 
5519740652SStephen Kelly   if (LHS.first.getEnd() < RHS.first.getEnd())
5619740652SStephen Kelly     return true;
5719740652SStephen Kelly   else if (LHS.first.getEnd() != RHS.first.getEnd())
5819740652SStephen Kelly     return false;
5919740652SStephen Kelly 
604f6d6984SStephen Kelly   return LocationCallFormatterCpp::format(*LHS.second) <
614f6d6984SStephen Kelly          LocationCallFormatterCpp::format(*RHS.second);
6219740652SStephen Kelly }
63b23abbeaSNathan James bool RangeLessThan::operator()(
64b23abbeaSNathan James     std::pair<SourceLocation, SharedLocationCall> const &LHS,
65b23abbeaSNathan James     std::pair<SourceLocation, SharedLocationCall> const &RHS) const {
66b23abbeaSNathan James   if (LHS.first == RHS.first)
674f6d6984SStephen Kelly     return LocationCallFormatterCpp::format(*LHS.second) <
684f6d6984SStephen Kelly            LocationCallFormatterCpp::format(*RHS.second);
69b23abbeaSNathan James   return LHS.first < RHS.first;
70b23abbeaSNathan James }
7119740652SStephen Kelly } // namespace internal
7219740652SStephen Kelly 
7319740652SStephen Kelly } // namespace tooling
7419740652SStephen Kelly } // namespace clang
7519740652SStephen Kelly 
7619740652SStephen Kelly #include "clang/Tooling/NodeIntrospection.inc"
77