ACAV f0ba4b7c9529
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 <vector>
30
31namespace acav {
32
33// Forward declarations
34class AstViewNode;
35
37struct Interval {
38 unsigned beginLine;
39 unsigned beginColumn;
40 unsigned endLine;
41 unsigned endColumn;
42 AstViewNode *node; // Not owned - AstContext owns all nodes
43
48 bool contains(unsigned line, unsigned column) const;
49
51 bool operator<(const Interval &other) const;
52};
53
63class IntervalTree {
64public:
65 IntervalTree() = default;
66 ~IntervalTree() = default;
67
68 // Non-copyable
69 IntervalTree(const IntervalTree &) = delete;
70 IntervalTree &operator=(const IntervalTree &) = delete;
71
74 void insert(Interval interval);
75
77 void finalize();
78
83 std::vector<AstViewNode *> query(unsigned line, unsigned column) const;
84
91 AstViewNode *queryFirstContained(unsigned beginLine, unsigned beginColumn,
92 unsigned endLine, unsigned endColumn) const;
93
95 std::size_t size() const { return intervals_.size(); }
96
97private:
98 std::vector<Interval> intervals_;
99 bool finalized_ = false;
100
101 unsigned getDepth(AstViewNode *node) const;
102};
103
108class SourceLocationIndex {
109public:
110 SourceLocationIndex() = default;
111 ~SourceLocationIndex() = default;
112
113 // Non-copyable (manages interval trees)
114 SourceLocationIndex(const SourceLocationIndex &) = delete;
115 SourceLocationIndex &operator=(const SourceLocationIndex &) = delete;
116
120 void addNode(AstViewNode *node);
121
123 void finalize();
124
130 std::vector<AstViewNode *> getNodesAt(FileID fileId, unsigned line,
131 unsigned column) const;
132
140 AstViewNode *getFirstNodeContainedInRange(FileID fileId, unsigned beginLine,
141 unsigned beginColumn,
142 unsigned endLine,
143 unsigned endColumn) const;
144
146 std::size_t getFileCount() const { return trees_.size(); }
147
149 std::size_t getTotalIntervals() const;
150
152 bool hasFile(FileID fileId) const;
153
154private:
155 std::map<FileID, IntervalTree> trees_; // One tree per file
156};
157
158} // 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.