Skip to content
This repository was archived by the owner on Dec 14, 2021. It is now read-only.

Commit 49e8458

Browse files
authored
Merge pull request #6 from CoatiSoftware/ambiguous_references
add methods to record edges as ambiguous
2 parents f14a8a0 + f78a718 commit 49e8458

13 files changed

+289
-4
lines changed

core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(LIB_SRC_FILES
1616
src/DatabaseStorage.cpp
1717
src/DefinitionKind.cpp
1818
src/EdgeKind.cpp
19+
src/ElementComponentKind.cpp
1920
src/LocationKind.cpp
2021
src/NameHierarchy.cpp
2122
src/NodeKind.cpp
@@ -32,6 +33,7 @@ set(LIB_HDR_FILES
3233
include/DatabaseStorage.h
3334
include/DefinitionKind.h
3435
include/EdgeKind.h
36+
include/ElementComponentKind.h
3537
include/LocationKind.h
3638
include/NameHierarchy.h
3739
include/NodeKind.h
@@ -40,6 +42,7 @@ set(LIB_HDR_FILES
4042
include/SourcetrailDBWriter.h
4143
include/SourcetrailException.h
4244
include/StorageEdge.h
45+
include/StorageElementComponent.h
4346
include/StorageError.h
4447
include/StorageFile.h
4548
include/StorageLocalSymbol.h

core/include/DatabaseStorage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "CppSQLite3.h"
2525

2626
#include "StorageEdge.h"
27+
#include "StorageElementComponent.h"
2728
#include "StorageError.h"
2829
#include "StorageFile.h"
2930
#include "StorageLocalSymbol.h"
@@ -61,6 +62,7 @@ namespace sourcetrail
6162
void rollbackTransaction();
6263
void optimizeDatabaseMemory();
6364

65+
int addElementComponent(const StorageElementComponentData& storageElementComponentData);
6466
int addNode(const StorageNodeData& storageNodeData);
6567
void addSymbol(const StorageSymbol& storageSymbol);
6668
void addFile(const StorageFile& storageFile);
@@ -102,6 +104,7 @@ namespace sourcetrail
102104
mutable CppSQLite3DB m_database;
103105

104106
CppSQLite3Statement m_insertElementStatement;
107+
CppSQLite3Statement m_insertElementComponentStatement;
105108
CppSQLite3Statement m_findNodeStatement;
106109
CppSQLite3Statement m_insertNodeStatement;
107110
CppSQLite3Statement m_setNodeTypeStmt;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2018 Coati Software KG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef SOURCETRAIL_ELEMENT_COMPONENT_KIND_H
18+
#define SOURCETRAIL_ELEMENT_COMPONENT_KIND_H
19+
20+
namespace sourcetrail
21+
{
22+
/**
23+
* Enum providing all possible values for kinds of node and edge components that can be stored to the Sourcetrail database.
24+
*/
25+
enum class ElementComponentKind : int
26+
{
27+
IS_AMBIGUOUS = 1 << 0
28+
};
29+
30+
int elementComponentKindToInt(ElementComponentKind kind);
31+
ElementComponentKind intToElementComponentKind(int i);
32+
}
33+
34+
#endif // SOURCETRAIL_ELEMENT_COMPONENT_KIND_H

core/include/LocationKind.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ namespace sourcetrail
3232
ATOMIC_RANGE = 5,
3333
INDEXER_ERROR = 6,
3434
FULLTEXT_SEARCH = 7,
35-
SCREEN_SEARCH = 8
35+
SCREEN_SEARCH = 8,
36+
UNSOLVED = 9
3637
};
3738

3839
int locationKindToInt(LocationKind kind);

core/include/SourcetrailDBWriter.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "DefinitionKind.h"
2424
#include "EdgeKind.h"
25+
#include "ElementComponentKind.h"
2526
#include "LocationKind.h"
2627
#include "NameHierarchy.h"
2728
#include "ReferenceKind.h"
@@ -349,6 +350,42 @@ namespace sourcetrail
349350
*/
350351
bool recordReferenceLocation(int referenceId, const SourceRange& location);
351352

353+
/**
354+
* Marks a reference that is stored in the database as "ambiguous"
355+
*
356+
* This method allows to additional information for a reference to the database. Sourcetrail will
357+
* display an "ambiguous" reference with a special style to emphasize that the existance of the
358+
* reference is questionable. This method is intended to be called in situations when an indexed
359+
* token may have meanings, all of which shall be recorded.
360+
*
361+
* param: referenceId - the id of the reference that shall be marked as ambiguous.
362+
*
363+
* return: true if successful. false on failure. getLastError() provides the error message.
364+
*/
365+
bool recordReferenceIsAmbiuous(int referenceId);
366+
367+
/**
368+
* Stores a location between a specific context and an "unsolved" symbol to the database
369+
*
370+
* This method allows to store all available information to the database in the case that a symbol
371+
* is referenced in a certain context but the referenced symbol could not be resolved to a concrete
372+
* name. For each reference recorded by this method, Sourcetrail's graph view will display an edge
373+
* that originates at the recorded context symbol and points to a node called "unsolved symbol".
374+
* Furthermore Sourcetrail's code view will use a different highlight when the provided source range
375+
* gets hovered.
376+
*
377+
* param: contextSymbolId - the id of the source of the recorded reference edge
378+
* param: referenceKind - kind of the recorded reference edge
379+
* param: location - the SourceRange that shall be recorded as location for the respective
380+
* reference.
381+
*
382+
* return: referenceId - integer id of the stored reference. 0 on failure. getLastError()
383+
* provides the error message.
384+
*
385+
* see: SourceRange
386+
*/
387+
int recordReferenceToUnsolvedSymhol(int contextSymbolId, ReferenceKind referenceKind, const SourceRange& location);
388+
352389
/**
353390
* Stores a location for the usage of a symbol's name as qualifier to the database
354391
*
@@ -475,6 +512,7 @@ namespace sourcetrail
475512
int addFile(const std::string& filePath);
476513
int addEdge(int sourceId, int targetId, EdgeKind edgeKind);
477514
void addSourceLocation(int elementId, const SourceRange& location, LocationKind kind);
515+
void addElementComponent(int elementId, ElementComponentKind kind, const std::string& data);
478516

479517
std::string m_projectFilePath;
480518
std::string m_databaseFilePath;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2018 Coati Software KG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef SOURCETRAIL_STORAGE_ELEMENT_COMPONENT_H
18+
#define SOURCETRAIL_STORAGE_ELEMENT_COMPONENT_H
19+
20+
#include <string>
21+
22+
namespace sourcetrail
23+
{
24+
struct StorageElementComponentData
25+
{
26+
StorageElementComponentData()
27+
: elementId(0)
28+
, componentKind(0)
29+
, data("")
30+
{}
31+
32+
StorageElementComponentData(int elementId, int componentKind, std::string data)
33+
: elementId(elementId)
34+
, componentKind(componentKind)
35+
, data(std::move(data))
36+
{}
37+
38+
int elementId;
39+
int componentKind;
40+
std::string data;
41+
};
42+
43+
struct StorageElementComponent : public StorageElementComponentData
44+
{
45+
StorageElementComponent()
46+
: StorageElementComponentData()
47+
, id(0)
48+
{}
49+
50+
StorageElementComponent(int id, const StorageElementComponentData& data)
51+
: StorageElementComponentData(data)
52+
, id(id)
53+
{}
54+
55+
StorageElementComponent(int id, int elementId, int componentKind, std::string data)
56+
: StorageElementComponentData(elementId, componentKind, data)
57+
, id(id)
58+
{}
59+
60+
int id;
61+
};
62+
}
63+
64+
#endif // SOURCETRAIL_STORAGE_ELEMENT_COMPONENT_H

core/src/DatabaseStorage.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ namespace sourcetrail
151151
executeStatement("VACUUM;");
152152
}
153153

154+
int DatabaseStorage::addElementComponent(const StorageElementComponentData& storageElementComponentData)
155+
{
156+
m_insertElementComponentStatement.bind(1, storageElementComponentData.elementId);
157+
m_insertElementComponentStatement.bind(2, storageElementComponentData.componentKind);
158+
m_insertElementComponentStatement.bind(3, storageElementComponentData.data.c_str());
159+
executeStatement(m_insertElementComponentStatement);
160+
int id = m_database.lastRowId();
161+
m_insertElementComponentStatement.reset();
162+
return id;
163+
}
164+
154165
int DatabaseStorage::addNode(const StorageNodeData& storageNodeData)
155166
{
156167
int id = 0;
@@ -393,6 +404,17 @@ namespace sourcetrail
393404
");"
394405
);
395406

407+
executeStatement(
408+
"CREATE TABLE IF NOT EXISTS element_component("
409+
" id INTEGER, "
410+
" element_id INTEGER, "
411+
" type INTEGER, "
412+
" data TEXT, "
413+
" PRIMARY KEY(id), "
414+
" FOREIGN KEY(element_id) REFERENCES element(id) ON DELETE CASCADE"
415+
");"
416+
);
417+
396418
executeStatement(
397419
"CREATE TABLE IF NOT EXISTS edge("
398420
" id INTEGER NOT NULL, "
@@ -518,6 +540,7 @@ namespace sourcetrail
518540
"symbol",
519541
"node",
520542
"edge",
543+
"element_component"
521544
"element"
522545
};
523546

@@ -557,6 +580,10 @@ namespace sourcetrail
557580
"INSERT INTO element(id) VALUES(NULL);"
558581
);
559582

583+
m_insertElementComponentStatement = compileStatement(
584+
"INSERT INTO element_component(id, element_id, type, data) VALUES(NULL, ?, ?, ?);"
585+
);
586+
560587
m_findNodeStatement = compileStatement(
561588
"SELECT id FROM node WHERE serialized_name == ? LIMIT 1;"
562589
);
@@ -648,6 +675,7 @@ namespace sourcetrail
648675
void DatabaseStorage::clearPrecompiledStatements()
649676
{
650677
m_insertElementStatement.finalize();
678+
m_insertElementComponentStatement.finalize();
651679
m_findNodeStatement.finalize();
652680
m_insertNodeStatement.finalize();
653681
m_setNodeTypeStmt.finalize();

core/src/ElementComponentKind.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2018 Coati Software KG
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "ElementComponentKind.h"
18+
19+
#include "SourcetrailException.h"
20+
21+
namespace sourcetrail
22+
{
23+
int elementComponentKindToInt(ElementComponentKind kind)
24+
{
25+
return static_cast<int>(kind);
26+
}
27+
28+
ElementComponentKind intToElementComponentKind(int i)
29+
{
30+
const ElementComponentKind kinds[] = {
31+
ElementComponentKind::IS_AMBIGUOUS
32+
};
33+
34+
for (ElementComponentKind kind : kinds)
35+
{
36+
if (i == elementComponentKindToInt(kind))
37+
{
38+
return kind;
39+
}
40+
}
41+
42+
throw SourcetrailException("Unable to convert integer \"" + std::to_string(i) + "\" to element component kind.");
43+
}
44+
}

core/src/LocationKind.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace sourcetrail
3636
LocationKind::ATOMIC_RANGE,
3737
LocationKind::INDEXER_ERROR,
3838
LocationKind::FULLTEXT_SEARCH,
39-
LocationKind::SCREEN_SEARCH
39+
LocationKind::SCREEN_SEARCH,
40+
LocationKind::UNSOLVED
4041
};
4142

4243
for (LocationKind kind : kinds)

0 commit comments

Comments
 (0)