ACAV 69797fb42a2b
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
SourceLocationIndex.h
Go to the documentation of this file.
1/*$!{
2* Aurora Clang AST Viewer (ACAV)
3*
4* Copyright (c) 2026 Min Liu
5* Copyright (c) 2026 Michael David Adams
6*
7* SPDX-License-Identifier: GPL-2.0-or-later
8*
9* This program is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License along
20* with this program; if not, see <https://www.gnu.org/licenses/>.
21}$!*/
22
25#pragma once
26
27#include "common/FileManager.h"
28#include <map>
29#include <memory>
30#include <vector>
31
32namespace acav {
33
34// Forward declarations
35class AstViewNode;
36
38struct Interval {
39 unsigned beginLine;
40 unsigned beginColumn;
41 unsigned endLine;
42 unsigned endColumn;
43 AstViewNode *node; // Not owned - AstContext owns all nodes
44
49 bool contains(unsigned line, unsigned column) const;
50
52 bool operator<(const Interval &other) const;
53};
54
65class IntervalTree {
66public:
67 IntervalTree() = default;
68 ~IntervalTree() = default;
69
70 // Non-copyable
71 IntervalTree(const IntervalTree &) = delete;
72 IntervalTree &operator=(const IntervalTree &) = delete;
73
76 void insert(Interval interval);
77
79 void finalize();
80
85 std::vector<AstViewNode *> query(unsigned line, unsigned column) const;
86
93 AstViewNode *queryFirstContained(unsigned beginLine, unsigned beginColumn,
94 unsigned endLine, unsigned endColumn) const;
95
97 std::size_t size() const { return intervals_.size(); }
98
99private:
100 struct SourcePoint {
101 unsigned line;
102 unsigned column;
103 };
104
105 struct QueryNode {
106 SourcePoint center;
107 std::vector<Interval> byBegin;
108 std::vector<Interval> byEndDescending;
109 std::unique_ptr<QueryNode> left;
110 std::unique_ptr<QueryNode> right;
111 };
112
113 std::vector<Interval> intervals_;
114 std::unique_ptr<QueryNode> queryRoot_;
115 bool finalized_ = false;
116
117 static std::unique_ptr<QueryNode>
118 buildQueryTree(std::vector<Interval> intervals);
119 static void queryPoint(const QueryNode *node, SourcePoint point,
120 std::vector<AstViewNode *> &results);
121 static SourcePoint beginPoint(const Interval &interval);
122 static SourcePoint endPoint(const Interval &interval);
123 static bool pointLess(SourcePoint lhs, SourcePoint rhs);
124 static bool startsAfter(const Interval &interval, SourcePoint point);
125 static bool endsBefore(const Interval &interval, SourcePoint point);
126 static bool intervalEndGreater(const Interval &lhs, const Interval &rhs);
127
128 unsigned getDepth(AstViewNode *node) const;
129};
130
135class SourceLocationIndex {
136public:
137 SourceLocationIndex() = default;
138 ~SourceLocationIndex() = default;
139
140 // Non-copyable (manages interval trees)
141 SourceLocationIndex(const SourceLocationIndex &) = delete;
142 SourceLocationIndex &operator=(const SourceLocationIndex &) = delete;
143
147 void addNode(AstViewNode *node);
148
150 void finalize();
151
157 std::vector<AstViewNode *> getNodesAt(FileID fileId, unsigned line,
158 unsigned column) const;
159
167 AstViewNode *getFirstNodeContainedInRange(FileID fileId, unsigned beginLine,
168 unsigned beginColumn,
169 unsigned endLine,
170 unsigned endColumn) const;
171
173 std::size_t getFileCount() const { return trees_.size(); }
174
176 std::size_t getTotalIntervals() const;
177
179 bool hasFile(FileID fileId) const;
180
181private:
182 std::map<FileID, IntervalTree> trees_; // One tree per file
183};
184
185} // namespace acav
Centralized file registry with path-to-FileID mapping.
std::size_t FileID
Type-safe identifier for registered files. 0 is reserved for invalid.
Definition FileManager.h:38
Represents node in AST tree hierarchy.
Definition AstNode.h:195
AstViewNode * queryFirstContained(unsigned beginLine, unsigned beginColumn, unsigned endLine, unsigned endColumn) const
Find first interval fully contained within a range.
void finalize()
Sort intervals by start position (call once after all inserts).
std::size_t size() const
Get total number of intervals.
std::vector< AstViewNode * > query(unsigned line, unsigned column) const
Find all intervals containing a point.
void insert(Interval interval)
Add interval to collection (not sorted yet).
void finalize()
Finalize all interval trees (call after all nodes added).
void addNode(AstViewNode *node)
Add node to index.
std::size_t getFileCount() const
Get number of files in index.
std::vector< AstViewNode * > getNodesAt(FileID fileId, unsigned line, unsigned column) const
Query nodes at specific source position.
bool hasFile(FileID fileId) const
Check whether the current TU AST contains any indexed nodes for a file.
std::size_t getTotalIntervals() const
Get total number of intervals across all files.
AstViewNode * getFirstNodeContainedInRange(FileID fileId, unsigned beginLine, unsigned beginColumn, unsigned endLine, unsigned endColumn) const
Query first node fully contained within a range.
Represents an interval in source code with associated AST node.
bool operator<(const Interval &other) const
Compare intervals by start position for sorting.
bool contains(unsigned line, unsigned column) const
Check if interval contains a point.