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

Commit

Permalink
Merge pull request #6 from CoatiSoftware/ambiguous_references
Browse files Browse the repository at this point in the history
add methods to record edges as ambiguous
  • Loading branch information
mlangkabel authored May 20, 2019
2 parents f14a8a0 + f78a718 commit 49e8458
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 4 deletions.
3 changes: 3 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(LIB_SRC_FILES
src/DatabaseStorage.cpp
src/DefinitionKind.cpp
src/EdgeKind.cpp
src/ElementComponentKind.cpp
src/LocationKind.cpp
src/NameHierarchy.cpp
src/NodeKind.cpp
Expand All @@ -32,6 +33,7 @@ set(LIB_HDR_FILES
include/DatabaseStorage.h
include/DefinitionKind.h
include/EdgeKind.h
include/ElementComponentKind.h
include/LocationKind.h
include/NameHierarchy.h
include/NodeKind.h
Expand All @@ -40,6 +42,7 @@ set(LIB_HDR_FILES
include/SourcetrailDBWriter.h
include/SourcetrailException.h
include/StorageEdge.h
include/StorageElementComponent.h
include/StorageError.h
include/StorageFile.h
include/StorageLocalSymbol.h
Expand Down
3 changes: 3 additions & 0 deletions core/include/DatabaseStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "CppSQLite3.h"

#include "StorageEdge.h"
#include "StorageElementComponent.h"
#include "StorageError.h"
#include "StorageFile.h"
#include "StorageLocalSymbol.h"
Expand Down Expand Up @@ -61,6 +62,7 @@ namespace sourcetrail
void rollbackTransaction();
void optimizeDatabaseMemory();

int addElementComponent(const StorageElementComponentData& storageElementComponentData);
int addNode(const StorageNodeData& storageNodeData);
void addSymbol(const StorageSymbol& storageSymbol);
void addFile(const StorageFile& storageFile);
Expand Down Expand Up @@ -102,6 +104,7 @@ namespace sourcetrail
mutable CppSQLite3DB m_database;

CppSQLite3Statement m_insertElementStatement;
CppSQLite3Statement m_insertElementComponentStatement;
CppSQLite3Statement m_findNodeStatement;
CppSQLite3Statement m_insertNodeStatement;
CppSQLite3Statement m_setNodeTypeStmt;
Expand Down
34 changes: 34 additions & 0 deletions core/include/ElementComponentKind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2018 Coati Software KG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef SOURCETRAIL_ELEMENT_COMPONENT_KIND_H
#define SOURCETRAIL_ELEMENT_COMPONENT_KIND_H

namespace sourcetrail
{
/**
* Enum providing all possible values for kinds of node and edge components that can be stored to the Sourcetrail database.
*/
enum class ElementComponentKind : int
{
IS_AMBIGUOUS = 1 << 0
};

int elementComponentKindToInt(ElementComponentKind kind);
ElementComponentKind intToElementComponentKind(int i);
}

#endif // SOURCETRAIL_ELEMENT_COMPONENT_KIND_H
3 changes: 2 additions & 1 deletion core/include/LocationKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ namespace sourcetrail
ATOMIC_RANGE = 5,
INDEXER_ERROR = 6,
FULLTEXT_SEARCH = 7,
SCREEN_SEARCH = 8
SCREEN_SEARCH = 8,
UNSOLVED = 9
};

int locationKindToInt(LocationKind kind);
Expand Down
38 changes: 38 additions & 0 deletions core/include/SourcetrailDBWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "DefinitionKind.h"
#include "EdgeKind.h"
#include "ElementComponentKind.h"
#include "LocationKind.h"
#include "NameHierarchy.h"
#include "ReferenceKind.h"
Expand Down Expand Up @@ -349,6 +350,42 @@ namespace sourcetrail
*/
bool recordReferenceLocation(int referenceId, const SourceRange& location);

/**
* Marks a reference that is stored in the database as "ambiguous"
*
* This method allows to additional information for a reference to the database. Sourcetrail will
* display an "ambiguous" reference with a special style to emphasize that the existance of the
* reference is questionable. This method is intended to be called in situations when an indexed
* token may have meanings, all of which shall be recorded.
*
* param: referenceId - the id of the reference that shall be marked as ambiguous.
*
* return: true if successful. false on failure. getLastError() provides the error message.
*/
bool recordReferenceIsAmbiuous(int referenceId);

/**
* Stores a location between a specific context and an "unsolved" symbol to the database
*
* This method allows to store all available information to the database in the case that a symbol
* is referenced in a certain context but the referenced symbol could not be resolved to a concrete
* name. For each reference recorded by this method, Sourcetrail's graph view will display an edge
* that originates at the recorded context symbol and points to a node called "unsolved symbol".
* Furthermore Sourcetrail's code view will use a different highlight when the provided source range
* gets hovered.
*
* param: contextSymbolId - the id of the source of the recorded reference edge
* param: referenceKind - kind of the recorded reference edge
* param: location - the SourceRange that shall be recorded as location for the respective
* reference.
*
* return: referenceId - integer id of the stored reference. 0 on failure. getLastError()
* provides the error message.
*
* see: SourceRange
*/
int recordReferenceToUnsolvedSymhol(int contextSymbolId, ReferenceKind referenceKind, const SourceRange& location);

/**
* Stores a location for the usage of a symbol's name as qualifier to the database
*
Expand Down Expand Up @@ -475,6 +512,7 @@ namespace sourcetrail
int addFile(const std::string& filePath);
int addEdge(int sourceId, int targetId, EdgeKind edgeKind);
void addSourceLocation(int elementId, const SourceRange& location, LocationKind kind);
void addElementComponent(int elementId, ElementComponentKind kind, const std::string& data);

std::string m_projectFilePath;
std::string m_databaseFilePath;
Expand Down
64 changes: 64 additions & 0 deletions core/include/StorageElementComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2018 Coati Software KG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef SOURCETRAIL_STORAGE_ELEMENT_COMPONENT_H
#define SOURCETRAIL_STORAGE_ELEMENT_COMPONENT_H

#include <string>

namespace sourcetrail
{
struct StorageElementComponentData
{
StorageElementComponentData()
: elementId(0)
, componentKind(0)
, data("")
{}

StorageElementComponentData(int elementId, int componentKind, std::string data)
: elementId(elementId)
, componentKind(componentKind)
, data(std::move(data))
{}

int elementId;
int componentKind;
std::string data;
};

struct StorageElementComponent : public StorageElementComponentData
{
StorageElementComponent()
: StorageElementComponentData()
, id(0)
{}

StorageElementComponent(int id, const StorageElementComponentData& data)
: StorageElementComponentData(data)
, id(id)
{}

StorageElementComponent(int id, int elementId, int componentKind, std::string data)
: StorageElementComponentData(elementId, componentKind, data)
, id(id)
{}

int id;
};
}

#endif // SOURCETRAIL_STORAGE_ELEMENT_COMPONENT_H
28 changes: 28 additions & 0 deletions core/src/DatabaseStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ namespace sourcetrail
executeStatement("VACUUM;");
}

int DatabaseStorage::addElementComponent(const StorageElementComponentData& storageElementComponentData)
{
m_insertElementComponentStatement.bind(1, storageElementComponentData.elementId);
m_insertElementComponentStatement.bind(2, storageElementComponentData.componentKind);
m_insertElementComponentStatement.bind(3, storageElementComponentData.data.c_str());
executeStatement(m_insertElementComponentStatement);
int id = m_database.lastRowId();
m_insertElementComponentStatement.reset();
return id;
}

int DatabaseStorage::addNode(const StorageNodeData& storageNodeData)
{
int id = 0;
Expand Down Expand Up @@ -393,6 +404,17 @@ namespace sourcetrail
");"
);

executeStatement(
"CREATE TABLE IF NOT EXISTS element_component("
" id INTEGER, "
" element_id INTEGER, "
" type INTEGER, "
" data TEXT, "
" PRIMARY KEY(id), "
" FOREIGN KEY(element_id) REFERENCES element(id) ON DELETE CASCADE"
");"
);

executeStatement(
"CREATE TABLE IF NOT EXISTS edge("
" id INTEGER NOT NULL, "
Expand Down Expand Up @@ -518,6 +540,7 @@ namespace sourcetrail
"symbol",
"node",
"edge",
"element_component"
"element"
};

Expand Down Expand Up @@ -557,6 +580,10 @@ namespace sourcetrail
"INSERT INTO element(id) VALUES(NULL);"
);

m_insertElementComponentStatement = compileStatement(
"INSERT INTO element_component(id, element_id, type, data) VALUES(NULL, ?, ?, ?);"
);

m_findNodeStatement = compileStatement(
"SELECT id FROM node WHERE serialized_name == ? LIMIT 1;"
);
Expand Down Expand Up @@ -648,6 +675,7 @@ namespace sourcetrail
void DatabaseStorage::clearPrecompiledStatements()
{
m_insertElementStatement.finalize();
m_insertElementComponentStatement.finalize();
m_findNodeStatement.finalize();
m_insertNodeStatement.finalize();
m_setNodeTypeStmt.finalize();
Expand Down
44 changes: 44 additions & 0 deletions core/src/ElementComponentKind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2018 Coati Software KG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ElementComponentKind.h"

#include "SourcetrailException.h"

namespace sourcetrail
{
int elementComponentKindToInt(ElementComponentKind kind)
{
return static_cast<int>(kind);
}

ElementComponentKind intToElementComponentKind(int i)
{
const ElementComponentKind kinds[] = {
ElementComponentKind::IS_AMBIGUOUS
};

for (ElementComponentKind kind : kinds)
{
if (i == elementComponentKindToInt(kind))
{
return kind;
}
}

throw SourcetrailException("Unable to convert integer \"" + std::to_string(i) + "\" to element component kind.");
}
}
3 changes: 2 additions & 1 deletion core/src/LocationKind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ namespace sourcetrail
LocationKind::ATOMIC_RANGE,
LocationKind::INDEXER_ERROR,
LocationKind::FULLTEXT_SEARCH,
LocationKind::SCREEN_SEARCH
LocationKind::SCREEN_SEARCH,
LocationKind::UNSOLVED
};

for (LocationKind kind : kinds)
Expand Down
Loading

0 comments on commit 49e8458

Please sign in to comment.