This commit is contained in:
mlangkabel
2018-11-30 16:16:31 +01:00
parent e105ac88ca
commit ea3abfc026
38 changed files with 187291 additions and 1 deletions
+69
View File
@@ -0,0 +1,69 @@
#ifndef SOURCETRAIL_DATABASE_STORAGE_H
#define SOURCETRAIL_DATABASE_STORAGE_H
#include <memory>
#include <string>
#include "sqlite/CppSQLite3.h"
namespace sourcetrail
{
/**
* Class wrapping the write and update interface of the Sourcetrail database.
*
* The DatabaseStorage provides the interface for writing and updating the Sourcetrail database. This interface only
* knows about basic data types, more elaborate types like enums, structs and classes need to be converted to basic
* types before using this interface.
*/
class DatabaseStorage
{
public:
static int getSupportedDatabaseVersion();
static std::shared_ptr<DatabaseStorage> openDatabase(const std::string& dbFilePath);
~DatabaseStorage();
void setupTables();
void clearTables();
bool isEmpty() const;
bool isCompatible() const;
int getLoadedDatabaseVersion() const;
void beginTransaction();
void commitTransaction();
void rollbackTransaction();
void optimizeDatabaseMemory();
int addNode(const std::string& serializedNameHierarchy);
void addSymbol(int symbolId, int definitionKind);
void addFile(int nodeId, const std::string& filePath);
int addEdge(int sourceId, int targetId, int edgeKind);
int addLocalSymbol(const std::string& name);
int addSourceLocation(
int fileId,
int startLineNumber,
int startColumnNumber,
int endLineNumber,
int endColumnNumber,
int locationKind);
void addOccurrence(int elementId, int sourceLocationId);
int addError(
const std::string& message,
bool fatal);
void setNodeType(int nodeId, int nodeKind);
private:
DatabaseStorage() = default;
void insertOrUpdateMetaValue(const std::string& key, const std::string& value);
void executeStatement(const std::string& statement) const;
void executeStatement(CppSQLite3Statement& statement) const;
CppSQLite3Query executeQuery(const std::string& query) const;
CppSQLite3Query executeQuery(CppSQLite3Statement& statement) const;
mutable CppSQLite3DB m_database;
};
}
#endif // SOURCETRAIL_DATABASE_STORAGE_H
+25
View File
@@ -0,0 +1,25 @@
#ifndef SOURCETRAIL_DEFINITION_KIND_H
#define SOURCETRAIL_DEFINITION_KIND_H
namespace sourcetrail
{
/**
* Enum providing all possible values for a symbol's definition kind.
*
* The DefinitionKind specifies "how" a symbol is defined.
* When recording the definition of a symbol, you would usually also record an explicit definition kind.
* However, you may also want to record symbols that are implicitly generated by the compiler. In this case you can
* record an implicit definition kind for those symbols.
* If you do not record any definition kind for a symbol, Sourcetrail will show it as "non-indexed".
*/
enum DefinitionKind
{
DEFINITION_IMPLICIT = 1,
DEFINITION_EXPLICIT = 2
};
int definitionKindToInt(DefinitionKind v);
DefinitionKind intToDefinitionKind(int v);
}
#endif // SOURCETRAIL_DEFINITION_KIND_H
+40
View File
@@ -0,0 +1,40 @@
#ifndef SOURCETRAIL_EDGE_KIND_H
#define SOURCETRAIL_EDGE_KIND_H
namespace sourcetrail
{
/**
* Enum providing all possible values for kinds of edges that can be stored to the Sourcetrail database.
*
* The DatabaseStorage provides the interface for writing and updating the Sourcetrail database. This interface only
* The EdgeKind enum should only be used internally and contains a complete list of values that may be written to
* the Sourcetrail database file. This enum contains kinds for edges that can be recorded explicitly, but also kinds
* for edges that will be created implicitly. For example edges of type "EDGE_MEMBER" will be created while storing parent
* child node relations when recording symbols with hierarchical names (see NameHierarchy).
*/
enum EdgeKind
{
EDGE_UNKNOWN = 0,
EDGE_MEMBER = 1 << 0,
EDGE_TYPE_USAGE = 1 << 1,
EDGE_USAGE = 1 << 2,
EDGE_CALL = 1 << 3,
EDGE_INHERITANCE = 1 << 4,
EDGE_OVERRIDE = 1 << 5,
EDGE_TEMPLATE_ARGUMENT = 1 << 6,
EDGE_TYPE_ARGUMENT = 1 << 7,
EDGE_TEMPLATE_DEFAULT_ARGUMENT = 1 << 8,
EDGE_TEMPLATE_SPECIALIZATION = 1 << 9,
EDGE_TEMPLATE_MEMBER_SPECIALIZATION = 1 << 10,
EDGE_INCLUDE = 1 << 11,
EDGE_IMPORT = 1 << 12,
EDGE_AGGREGATION = 1 << 13,
EDGE_MACRO_USAGE = 1 << 14,
EDGE_ANNOTATION_USAGE = 1 << 15
};
int edgeKindToInt(EdgeKind edgeKind);
EdgeKind intToEdgeKind(int i);
}
#endif // SOURCETRAIL_EDGE_KIND_H
+26
View File
@@ -0,0 +1,26 @@
#ifndef SOURCETRAIL_LOCATION_KIND_H
#define SOURCETRAIL_LOCATION_KIND_H
namespace sourcetrail
{
/**
* Enum providing all possible values for kinds of locations that can be stored to the Sourcetrail database.
*/
enum LocationKind
{
LOCATION_TOKEN = 0,
LOCATION_SCOPE = 1,
LOCATION_QUALIFIER = 2,
LOCATION_LOCAL_SYMBOL = 3,
LOCATION_SIGNATURE = 4,
LOCATION_COMMENT = 5,
LOCATION_ERROR = 6,
LOCATION_FULLTEXT_SEARCH = 7,
LOCATION_SCREEN_SEARCH = 8
};
LocationKind intToLocationKind(int i);
int locationKindToInt(LocationKind locationKind);
}
#endif // SOURCETRAIL_LOCATION_KIND_H
+19
View File
@@ -0,0 +1,19 @@
#ifndef SOURCETRAIL_NAME_ELEMENT_H
#define SOURCETRAIL_NAME_ELEMENT_H
#include <string>
namespace sourcetrail
{
/**
* Struct that represents a single hierarchical element that is part of a symbol's name.
*/
struct NameElement
{
std::string prefix;
std::string name;
std::string postfix;
};
}
#endif // SOURCETRAIL_NAME_ELEMENT_H
+24
View File
@@ -0,0 +1,24 @@
#ifndef SOURCETRAIL_NAME_HIERARCHY_H
#define SOURCETRAIL_NAME_HIERARCHY_H
#include <string>
#include <vector>
#include "NameElement.h"
namespace sourcetrail
{
/**
* Struct that represents an entire name of a symbol.
*/
struct NameHierarchy
{
std::string nameDelimiter;
std::vector<NameElement> nameElements;
};
std::string serializeNameHierarchyToJson(const NameHierarchy& nameHierarchy);
NameHierarchy deserializeNameHierarchyFromJson(const std::string& serializedNameHierarchy);
}
#endif // SOURCETRAIL_NAME_HIERARCHY_H
+38
View File
@@ -0,0 +1,38 @@
#ifndef SOURCETRAIL_NODE_KIND_H
#define SOURCETRAIL_NODE_KIND_H
namespace sourcetrail
{
/**
* Enum providing all possible values for kinds of nodes that can be stored to the Sourcetrail database.
*/
enum NodeKind
{
NODE_UNKNOWN = 1 << 0,
NODE_TYPE = 1 << 1,
NODE_BUILTIN_TYPE = 1 << 2,
NODE_NAMESPACE = 1 << 3,
NODE_PACKAGE = 1 << 4,
NODE_STRUCT = 1 << 5,
NODE_CLASS = 1 << 6,
NODE_INTERFACE = 1 << 7,
NODE_ANNOTATION = 1 << 8,
NODE_GLOBAL_VARIABLE = 1 << 9,
NODE_FIELD = 1 << 10,
NODE_FUNCTION = 1 << 11,
NODE_METHOD = 1 << 12,
NODE_ENUM = 1 << 13,
NODE_ENUM_CONSTANT = 1 << 14,
NODE_TYPEDEF = 1 << 15,
NODE_TEMPLATE_PARAMETER = 1 << 16,
NODE_TYPE_PARAMETER = 1 << 17,
NODE_FILE = 1 << 18,
NODE_MACRO = 1 << 19,
NODE_UNION = 1 << 20,
};
int nodeKindToInt(NodeKind v);
NodeKind intToNodeKind(int v);
}
#endif // SOURCETRAIL_NODE_KIND_H
+32
View File
@@ -0,0 +1,32 @@
#ifndef SOURCETRAIL_REFERENCE_KIND_H
#define SOURCETRAIL_REFERENCE_KIND_H
namespace sourcetrail
{
enum EdgeKind;
/**
* Enum providing all possible values for kinds of references that can be recorded using the SourcetrailDBWriter interface.
*/
enum ReferenceKind
{
REFERENCE_TYPE_USAGE,
REFERENCE_USAGE,
REFERENCE_CALL,
REFERENCE_INHERITANCE,
REFERENCE_OVERRIDE,
REFERENCE_TEMPLATE_ARGUMENT,
REFERENCE_TYPE_ARGUMENT,
REFERENCE_TEMPLATE_DEFAULT_ARGUMENT,
REFERENCE_TEMPLATE_SPECIALIZATION,
REFERENCE_TEMPLATE_MEMBER_SPECIALIZATION,
REFERENCE_INCLUDE,
REFERENCE_IMPORT,
REFERENCE_MACRO_USAGE,
REFERENCE_ANNOTATION_USAGE
};
EdgeKind referenceKindToEdgeKind(ReferenceKind referenceKind);
}
#endif // SOURCETRAIL_REFERENCE_KIND_H
+21
View File
@@ -0,0 +1,21 @@
#ifndef SOURCETRAIL_SOURCE_LOCATION_H
#define SOURCETRAIL_SOURCE_LOCATION_H
#include <string>
namespace sourcetrail
{
/**
* Struct that represents a single character location in a source file.
*
* Note: Line and column numbers start at 1 instead of 0!
*/
struct SourceLocation
{
std::string filePath;
int line;
int column;
};
}
#endif // SOURCETRAIL_SOURCE_LOCATION_H
+23
View File
@@ -0,0 +1,23 @@
#ifndef SOURCETRAIL_SOURCE_RANGE_H
#define SOURCETRAIL_SOURCE_RANGE_H
#include <string>
namespace sourcetrail
{
/**
* Struct that represents the location of a range of characters in a source file.
*
* Note: Line and column numbers start at 1 instead of 0!
*/
struct SourceRange
{
std::string filePath;
int startLine;
int startColumn;
int endLine;
int endColumn;
};
}
#endif // SOURCETRAIL_SOURCE_RANGE_H
+95
View File
@@ -0,0 +1,95 @@
#ifndef SOURCETRAIL_SRCTRLDB_WRITER_H
#define SOURCETRAIL_SRCTRLDB_WRITER_H
#include <memory>
#include <string>
namespace sourcetrail
{
class DatabaseStorage;
struct SourceRange;
struct NameHierarchy;
enum DefinitionKind;
enum EdgeKind;
enum LocationKind;
enum ReferenceKind;
enum SymbolKind;
/**
* Class wrapping the main interface for writing data to a Sourcetrail project database.
*
* TODO: write a detailed description here.
* TODO: document all public interface methods
* TODO: add small example for usage here
*/
class SourcetrailDBWriter
{
public:
SourcetrailDBWriter();
std::string getVersionString() const;
int getSupportedDatabaseVersion() const;
const std::string& getLastError() const;
void clearLastError();
bool openProject(const std::string& projectDirectory, const std::string& projectName);
bool closeProject();
bool clearProject();
bool isEmpty() const;
bool isCompatible() const;
int getLoadedDatabaseVersion() const;
bool beginTransaction();
bool commitTransaction();
bool rollbackTransaction();
bool optimizeDatabaseMemory();
/// will return the same id for the same name hierarchy. will return 0 on failure. 0 is not a valid symbol id
int recordSymbol(const NameHierarchy& nameHierarchy);
bool recordSymbolDefinitionKind(int symbolId, DefinitionKind definitionKind);
bool recordSymbolKind(int symbolId, SymbolKind symbolKind);
/// The provided "location" will be clickable and displayable. When clicked it will cause the symbol with the specified "symbolId" to be activated. When
/// the symbol with the specified "symbolId" is activated, this location will be displayed.
bool recordSymbolLocation(int symbolId, const SourceRange& location);
/// The provided "location" will be displayable. When the symbol with the specified "symbolId" is activated, this location will be displayed.
bool recordSymbolScopeLocation(int symbolId, const SourceRange& location);
bool recordSymbolSignatureLocation(int symbolId, const SourceRange& location);
int recordReference(int contextSymbolId, int referencedSymbolId, ReferenceKind referenceKind);
bool recordReferenceLocation(int referenceId, const SourceRange& location);
int recordFile(const std::string& filePath);
int recordLocalSymbol(const std::string& name);
bool recordLocalSymbolLocation(int localSymbolId, const SourceRange& location);
bool recordCommentLocation(const SourceRange& location);
bool recordError(const std::string& message, bool fatal, const SourceRange& location);
private:
static std::string serializeNameHierarchy(const NameHierarchy& nameHierarchy);
std::string getProjectFilePath() const;
std::string getDatabaseFilePath() const;
void openDatabase();
void closeDatabase();
void setupDatabaseTables();
void clearDatabaseTables();
void createOrResetProjectFile();
int addNodeHierarchy(const NameHierarchy& nameHierarchy);
int addFile(const std::string& filePath);
int addEdge(int sourceId, int targetId, EdgeKind edgeKind);
void addSourceLocation(int elementId, const SourceRange& location, LocationKind kind);
std::string m_projectDirectory;
std::string m_projectName;
std::shared_ptr<DatabaseStorage> m_storage;
mutable std::string m_lastError;
};
}
#endif // SOURCETRAIL_SRCTRLDB_WRITER_H
+23
View File
@@ -0,0 +1,23 @@
#ifndef SOURCETRAIL_SOURCETRAIL_EXCEPTION_H
#define SOURCETRAIL_SOURCETRAIL_EXCEPTION_H
#include <string>
namespace sourcetrail
{
/**
* Exception type that is thrown by the SourcetrailDBWriter API.
*/
class SourcetrailException
{
public:
SourcetrailException(std::string message) : m_message(message) {}
virtual ~SourcetrailException() = default;
std::string getMessage() const { return m_message; }
private:
std::string m_message;
};
}
#endif // SOURCETRAIL_SOURCETRAIL_EXCEPTION_H
+37
View File
@@ -0,0 +1,37 @@
#ifndef SOURCETRAIL_SYMBOL_KIND_H
#define SOURCETRAIL_SYMBOL_KIND_H
namespace sourcetrail
{
enum NodeKind;
/**
* Enum providing all possible values for kinds of symbols that can be recorded using the SourcetrailDBWriter interface.
*/
enum SymbolKind
{
SYMBOL_TYPE,
SYMBOL_BUILTIN_TYPE,
SYMBOL_NAMESPACE,
SYMBOL_PACKAGE,
SYMBOL_STRUCT,
SYMBOL_CLASS,
SYMBOL_INTERFACE,
SYMBOL_ANNOTATION,
SYMBOL_GLOBAL_VARIABLE,
SYMBOL_FIELD,
SYMBOL_FUNCTION,
SYMBOL_METHOD,
SYMBOL_ENUM,
SYMBOL_ENUM_CONSTANT,
SYMBOL_TYPEDEF,
SYMBOL_TEMPLATE_PARAMETER,
SYMBOL_TYPE_PARAMETER,
SYMBOL_MACRO,
SYMBOL_UNION,
};
NodeKind symbolKindToNodeKind(SymbolKind v);
}
#endif // SOURCETRAIL_SYMBOL_KIND_H
+19
View File
@@ -0,0 +1,19 @@
#ifndef SOURCETRAIL_UTILITY_H
#define SOURCETRAIL_UTILITY_H
#include <string>
#include <time.h>
namespace sourcetrail
{
namespace utility
{
bool getFileExists(const std::string& filePath);
std::string getFileContent(const std::string& filePath);
time_t getFileModificationTime(const std::string& filePath);
std::string getDateTimeString(const time_t& time);
int getLineCount(const std::string s);
}
}
#endif // SOURCETRAIL_UTILITY_H