From 0251b6d8a7e8c82089c641c2bb0af42a05712daf Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Mon, 25 Jan 2021 20:19:36 +0100 Subject: [PATCH] apply clang-format --- core/.clang-format | 82 ++ core/include/DatabaseStorage.h | 228 +-- core/include/DefinitionKind.h | 64 +- core/include/EdgeKind.h | 86 +- core/include/ElementComponentKind.h | 50 +- core/include/LocationKind.h | 68 +- core/include/NameHierarchy.h | 166 +-- core/include/NodeKind.h | 90 +- core/include/ReferenceKind.h | 68 +- core/include/SourceRange.h | 60 +- core/include/SourcetrailDBWriter.h | 932 ++++++------ core/include/SourcetrailException.h | 57 +- core/include/StorageEdge.h | 82 +- core/include/StorageElementComponent.h | 85 +- core/include/StorageError.h | 104 +- core/include/StorageFile.h | 76 +- core/include/StorageLocalSymbol.h | 71 +- core/include/StorageNode.h | 75 +- core/include/StorageOccurrence.h | 52 +- core/include/StorageSourceLocation.h | 106 +- core/include/StorageSymbol.h | 52 +- core/include/SymbolKind.h | 84 +- core/include/utility.h | 46 +- core/src/DatabaseStorage.cpp | 1814 ++++++++++++------------ core/src/DefinitionKind.cpp | 66 +- core/src/EdgeKind.cpp | 89 +- core/src/ElementComponentKind.cpp | 68 +- core/src/LocationKind.cpp | 87 +- core/src/NameHierarchy.cpp | 231 ++- core/src/NodeKind.cpp | 108 +- core/src/ReferenceKind.cpp | 82 +- core/src/SourcetrailDBWriter.cpp | 1556 ++++++++++---------- core/src/SymbolKind.cpp | 114 +- core/src/utility.cpp | 114 +- 34 files changed, 3492 insertions(+), 3621 deletions(-) create mode 100644 core/.clang-format diff --git a/core/.clang-format b/core/.clang-format new file mode 100644 index 0000000..a8ec2b7 --- /dev/null +++ b/core/.clang-format @@ -0,0 +1,82 @@ +AccessModifierOffset: -4 +AlignAfterOpenBracket: AlwaysBreak +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignConsecutiveMacros: false +AlignEscapedNewlines: Right +AlignOperands: false +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: false +BinPackParameters: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Allman +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +BreakStringLiterals: true +ColumnLimit: 140 +CompactNamespaces: true +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +FixNamespaceComments: true +IncludeBlocks: Preserve +IndentCaseLabels: false +IndentPPDirectives: AfterHash +IndentWidth: 4 +IndentWrappedFunctionNames: true +KeepEmptyLinesAtTheStartOfBlocks: false +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: None +PenaltyBreakAssignment: 50 +PenaltyBreakBeforeFirstCallParameter: 0 +PenaltyBreakComment: 100 +PenaltyBreakFirstLessLess: 200 +PenaltyBreakString: 100 +PenaltyBreakTemplateDeclaration: 0 +PenaltyExcessCharacter: 10 +PenaltyReturnTypeOnItsOwnLine: 1000 +PointerAlignment: Left +ReflowComments: true +SortIncludes: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: true +SpaceBeforeCtorInitializerColon: false +SpaceBeforeInheritanceColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 4 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: c++17 +TabWidth: 4 +UseTab: Always +--- +Language: Cpp +--- +Language: Java +#BasedOnStyle: Google +#BreakAfterJavaFieldAnnotations: true diff --git a/core/include/DatabaseStorage.h b/core/include/DatabaseStorage.h index 5365587..6e393f8 100644 --- a/core/include/DatabaseStorage.h +++ b/core/include/DatabaseStorage.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_DATABASE_STORAGE_H #define SOURCETRAIL_DATABASE_STORAGE_H @@ -35,112 +35,112 @@ 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 +/** + * 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::unique_ptr openDatabase(const std::string& dbFilePath); + ~DatabaseStorage(); + + void setupDatabase(); + void clearDatabase(); + + void setProjectSettingsText(const std::string& text); + + bool isEmpty() const; + bool isCompatible() const; + int getLoadedDatabaseVersion() const; + void beginTransaction(); + void commitTransaction(); + void rollbackTransaction(); + void optimizeDatabaseMemory(); + + int addElementComponent(const StorageElementComponentData& storageElementComponentData); + int addNode(const StorageNodeData& storageNodeData); + void addSymbol(const StorageSymbol& storageSymbol); + void addFile(const StorageFile& storageFile); + int addEdge(const StorageEdgeData& storageEdgeData); + int addLocalSymbol(const StorageLocalSymbolData& storageLocalSymbolData); + int addSourceLocation(const StorageSourceLocationData& storageSourceLocationData); + void addOccurrence(const StorageOccurrence& storageOccurrence); + int addError(const StorageErrorData& storageErrorData); + + void setNodeType(int nodeId, int nodeKind); + void setFileLanguage(int fileId, const std::string& languageIdentifier); + + template + std::vector getAll() const { - public: - static int getSupportedDatabaseVersion(); - static std::unique_ptr openDatabase(const std::string& dbFilePath); - ~DatabaseStorage(); + return doGetAll(""); + } - void setupDatabase(); - void clearDatabase(); +private: + DatabaseStorage() = default; - void setProjectSettingsText(const std::string& text); + void setupTables(); + void clearTables(); + void setupIndices(); + void setupPrecompiledStatements(); + void clearPrecompiledStatements(); - bool isEmpty() const; - bool isCompatible() const; - int getLoadedDatabaseVersion() const; - void beginTransaction(); - void commitTransaction(); - void rollbackTransaction(); - void optimizeDatabaseMemory(); + int insertElement(); + void insertOrUpdateMetaValue(const std::string& key, const std::string& value); + CppSQLite3Statement compileStatement(const std::string& statement) const; + void executeStatement(const std::string& statement) const; + void executeStatement(CppSQLite3Statement& statement) const; + CppSQLite3Query executeQuery(const std::string& query) const; + CppSQLite3Query executeQuery(CppSQLite3Statement& statement) const; - int addElementComponent(const StorageElementComponentData& storageElementComponentData); - int addNode(const StorageNodeData& storageNodeData); - void addSymbol(const StorageSymbol& storageSymbol); - void addFile(const StorageFile& storageFile); - int addEdge(const StorageEdgeData& storageEdgeData); - int addLocalSymbol(const StorageLocalSymbolData& storageLocalSymbolData); - int addSourceLocation(const StorageSourceLocationData& storageSourceLocationData); - void addOccurrence(const StorageOccurrence& storageOccurrence); - int addError(const StorageErrorData& storageErrorData); + template + std::vector doGetAll(const std::string& query) const; - void setNodeType(int nodeId, int nodeKind); - void setFileLanguage(int fileId, const std::string& languageIdentifier); + mutable CppSQLite3DB m_database; - template - std::vector getAll() const - { - return doGetAll(""); - } + CppSQLite3Statement m_insertElementStatement; + CppSQLite3Statement m_insertElementComponentStatement; + CppSQLite3Statement m_findNodeStatement; + CppSQLite3Statement m_insertNodeStatement; + CppSQLite3Statement m_setNodeTypeStmt; + CppSQLite3Statement m_insertSymbolStatement; + CppSQLite3Statement m_findFileStatement; + CppSQLite3Statement m_insertFileStatement; + CppSQLite3Statement m_setFileLanguageStmt; + CppSQLite3Statement m_insertFileContentStatement; + CppSQLite3Statement m_findEdgeStatement; + CppSQLite3Statement m_insertEdgeStatement; + CppSQLite3Statement m_findLocalSymbolStmt; + CppSQLite3Statement m_insertLocalSymbolStmt; + CppSQLite3Statement m_findSourceLocationStmt; + CppSQLite3Statement m_insertSourceLocationStmt; + CppSQLite3Statement m_insertOccurenceStmt; + CppSQLite3Statement m_findErrorStatement; + CppSQLite3Statement m_insertErrorStatement; + CppSQLite3Statement m_insertOrUpdateMetaValueStmt; +}; - private: - DatabaseStorage() = default; +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const; +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const; +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const; +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const; +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const; +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const; +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const; +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const; +} // namespace sourcetrail - void setupTables(); - void clearTables(); - void setupIndices(); - void setupPrecompiledStatements(); - void clearPrecompiledStatements(); - - int insertElement(); - void insertOrUpdateMetaValue(const std::string& key, const std::string& value); - CppSQLite3Statement compileStatement(const std::string& statement) const; - void executeStatement(const std::string& statement) const; - void executeStatement(CppSQLite3Statement& statement) const; - CppSQLite3Query executeQuery(const std::string& query) const; - CppSQLite3Query executeQuery(CppSQLite3Statement& statement) const; - - template - std::vector doGetAll(const std::string& query) const; - - mutable CppSQLite3DB m_database; - - CppSQLite3Statement m_insertElementStatement; - CppSQLite3Statement m_insertElementComponentStatement; - CppSQLite3Statement m_findNodeStatement; - CppSQLite3Statement m_insertNodeStatement; - CppSQLite3Statement m_setNodeTypeStmt; - CppSQLite3Statement m_insertSymbolStatement; - CppSQLite3Statement m_findFileStatement; - CppSQLite3Statement m_insertFileStatement; - CppSQLite3Statement m_setFileLanguageStmt; - CppSQLite3Statement m_insertFileContentStatement; - CppSQLite3Statement m_findEdgeStatement; - CppSQLite3Statement m_insertEdgeStatement; - CppSQLite3Statement m_findLocalSymbolStmt; - CppSQLite3Statement m_insertLocalSymbolStmt; - CppSQLite3Statement m_findSourceLocationStmt; - CppSQLite3Statement m_insertSourceLocationStmt; - CppSQLite3Statement m_insertOccurenceStmt; - CppSQLite3Statement m_findErrorStatement; - CppSQLite3Statement m_insertErrorStatement; - CppSQLite3Statement m_insertOrUpdateMetaValueStmt; - }; - - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const; - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const; - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const; - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const; - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const; - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const; - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const; - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const; -} - -#endif // SOURCETRAIL_DATABASE_STORAGE_H +#endif // SOURCETRAIL_DATABASE_STORAGE_H diff --git a/core/include/DefinitionKind.h b/core/include/DefinitionKind.h index 4a8650b..6e79f80 100644 --- a/core/include/DefinitionKind.h +++ b/core/include/DefinitionKind.h @@ -1,41 +1,41 @@ /* -* 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. -*/ + * 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_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 class DefinitionKind : int - { - IMPLICIT = 1, - EXPLICIT = 2 - }; +/** + * 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 class DefinitionKind : int +{ + IMPLICIT = 1, + EXPLICIT = 2 +}; - int definitionKindToInt(DefinitionKind kind); - DefinitionKind intToDefinitionKind(int i); -} +int definitionKindToInt(DefinitionKind kind); +DefinitionKind intToDefinitionKind(int i); +} // namespace sourcetrail -#endif // SOURCETRAIL_DEFINITION_KIND_H +#endif // SOURCETRAIL_DEFINITION_KIND_H diff --git a/core/include/EdgeKind.h b/core/include/EdgeKind.h index 445da5c..adcb4d0 100644 --- a/core/include/EdgeKind.h +++ b/core/include/EdgeKind.h @@ -1,52 +1,52 @@ /* -* 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. -*/ + * 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_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 class EdgeKind : int - { - UNKNOWN = 0, - MEMBER = 1 << 0, - TYPE_USAGE = 1 << 1, - USAGE = 1 << 2, - CALL = 1 << 3, - INHERITANCE = 1 << 4, - OVERRIDE = 1 << 5, - TYPE_ARGUMENT = 1 << 6, - TEMPLATE_SPECIALIZATION = 1 << 7, - INCLUDE = 1 << 8, - IMPORT = 1 << 9, - MACRO_USAGE = 1 << 11, // needs new db version if decremented - ANNOTATION_USAGE = 1 << 12 - }; +/** + * 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 class EdgeKind : int +{ + UNKNOWN = 0, + MEMBER = 1 << 0, + TYPE_USAGE = 1 << 1, + USAGE = 1 << 2, + CALL = 1 << 3, + INHERITANCE = 1 << 4, + OVERRIDE = 1 << 5, + TYPE_ARGUMENT = 1 << 6, + TEMPLATE_SPECIALIZATION = 1 << 7, + INCLUDE = 1 << 8, + IMPORT = 1 << 9, + MACRO_USAGE = 1 << 11, // needs new db version if decremented + ANNOTATION_USAGE = 1 << 12 +}; - int edgeKindToInt(EdgeKind kind); - EdgeKind intToEdgeKind(int i); -} +int edgeKindToInt(EdgeKind kind); +EdgeKind intToEdgeKind(int i); +} // namespace sourcetrail -#endif // SOURCETRAIL_EDGE_KIND_H +#endif // SOURCETRAIL_EDGE_KIND_H diff --git a/core/include/ElementComponentKind.h b/core/include/ElementComponentKind.h index a688c30..55a9d9f 100644 --- a/core/include/ElementComponentKind.h +++ b/core/include/ElementComponentKind.h @@ -1,34 +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. -*/ + * 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 - }; +/** + * 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); -} +int elementComponentKindToInt(ElementComponentKind kind); +ElementComponentKind intToElementComponentKind(int i); +} // namespace sourcetrail -#endif // SOURCETRAIL_ELEMENT_COMPONENT_KIND_H +#endif // SOURCETRAIL_ELEMENT_COMPONENT_KIND_H diff --git a/core/include/LocationKind.h b/core/include/LocationKind.h index 196ea3e..e914741 100644 --- a/core/include/LocationKind.h +++ b/core/include/LocationKind.h @@ -1,43 +1,43 @@ /* -* 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. -*/ + * 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_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 class LocationKind : int - { - TOKEN = 0, - SCOPE = 1, - QUALIFIER = 2, - LOCAL_SYMBOL = 3, - SIGNATURE = 4, - ATOMIC_RANGE = 5, - INDEXER_ERROR = 6, - FULLTEXT_SEARCH = 7, - SCREEN_SEARCH = 8, - UNSOLVED = 9 - }; +/** + * Enum providing all possible values for kinds of locations that can be stored to the Sourcetrail database. + */ +enum class LocationKind : int +{ + TOKEN = 0, + SCOPE = 1, + QUALIFIER = 2, + LOCAL_SYMBOL = 3, + SIGNATURE = 4, + ATOMIC_RANGE = 5, + INDEXER_ERROR = 6, + FULLTEXT_SEARCH = 7, + SCREEN_SEARCH = 8, + UNSOLVED = 9 +}; - int locationKindToInt(LocationKind kind); - LocationKind intToLocationKind(int i); -} +int locationKindToInt(LocationKind kind); +LocationKind intToLocationKind(int i); +} // namespace sourcetrail -#endif // SOURCETRAIL_LOCATION_KIND_H +#endif // SOURCETRAIL_LOCATION_KIND_H diff --git a/core/include/NameHierarchy.h b/core/include/NameHierarchy.h index b69d504..8d61922 100644 --- a/core/include/NameHierarchy.h +++ b/core/include/NameHierarchy.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_NAME_HIERARCHY_H #define SOURCETRAIL_NAME_HIERARCHY_H @@ -22,77 +22,77 @@ namespace sourcetrail { - /** - * Struct that represents a single hierarchical element that is part of a symbol's name. - * - * prefix: optional prefix used for unique identification and shown in tooltips - * name: name represented by this element - * postfix: optional prefix used for unique identification and shown in tooltips - */ - struct NameElement - { - std::string prefix; - std::string name; - std::string postfix; - }; +/** + * Struct that represents a single hierarchical element that is part of a symbol's name. + * + * prefix: optional prefix used for unique identification and shown in tooltips + * name: name represented by this element + * postfix: optional prefix used for unique identification and shown in tooltips + */ +struct NameElement +{ + std::string prefix; + std::string name; + std::string postfix; +}; - /** - * Struct that represents an entire name of a symbol. - * - * nameDelimiter: delimiter added between name elements - * nameElements: all name elements that make up the hierarchy - */ - struct NameHierarchy - { - std::string nameDelimiter; - std::vector nameElements; - }; +/** + * Struct that represents an entire name of a symbol. + * + * nameDelimiter: delimiter added between name elements + * nameElements: all name elements that make up the hierarchy + */ +struct NameHierarchy +{ + std::string nameDelimiter; + std::vector nameElements; +}; - /** - * Converts a NameHierarchy to a JSON string - * - * param: nameHierarchy - the name hierarchy to convert to JSON string - * - * return: a JSON object of the form: - * { - * "name_delimiter" : "." - * "name_elements" : [ - * { - * "prefix" : "", - * "name" : "", - * "postfix" : "" - * }, - * ... - * ] - * } - */ - std::string serializeNameHierarchyToJson(const NameHierarchy& nameHierarchy); +/** + * Converts a NameHierarchy to a JSON string + * + * param: nameHierarchy - the name hierarchy to convert to JSON string + * + * return: a JSON object of the form: + * { + * "name_delimiter" : "." + * "name_elements" : [ + * { + * "prefix" : "", + * "name" : "", + * "postfix" : "" + * }, + * ... + * ] + * } + */ +std::string serializeNameHierarchyToJson(const NameHierarchy& nameHierarchy); - /** - * Converts a JSON string to a NameHierarchy - * - * param: serializedNameHierarchy - JSON object string of the form: - * { - * "name_delimiter" : "." - * "name_elements" : [ - * { - * "prefix" : "", - * "name" : "", - * "postfix" : "" - * }, - * ... - * ] - * } - * param: error - optional pointer to a string, where an error message will be set - * - * return: NameHierarchy object. Empty on failure. - */ - NameHierarchy deserializeNameHierarchyFromJson(const std::string& serializedNameHierarchy, std::string* error = nullptr); +/** + * Converts a JSON string to a NameHierarchy + * + * param: serializedNameHierarchy - JSON object string of the form: + * { + * "name_delimiter" : "." + * "name_elements" : [ + * { + * "prefix" : "", + * "name" : "", + * "postfix" : "" + * }, + * ... + * ] + * } + * param: error - optional pointer to a string, where an error message will be set + * + * return: NameHierarchy object. Empty on failure. + */ +NameHierarchy deserializeNameHierarchyFromJson(const std::string& serializedNameHierarchy, std::string* error = nullptr); - /** - * INTERNAL: Converts a NameHierarchy to a string in Sourcetrail database format - */ - std::string serializeNameHierarchyToDatabaseString(const NameHierarchy& nameHierarchy); -} +/** + * INTERNAL: Converts a NameHierarchy to a string in Sourcetrail database format + */ +std::string serializeNameHierarchyToDatabaseString(const NameHierarchy& nameHierarchy); +} // namespace sourcetrail -#endif // SOURCETRAIL_NAME_HIERARCHY_H +#endif // SOURCETRAIL_NAME_HIERARCHY_H diff --git a/core/include/NodeKind.h b/core/include/NodeKind.h index 7f8ac53..3487aba 100644 --- a/core/include/NodeKind.h +++ b/core/include/NodeKind.h @@ -1,54 +1,54 @@ /* -* 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. -*/ + * 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_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 class NodeKind : int - { - UNKNOWN = 1 << 0, - TYPE = 1 << 1, - BUILTIN_TYPE = 1 << 2, - MODULE = 1 << 3, - NAMESPACE = 1 << 4, - PACKAGE = 1 << 5, - STRUCT = 1 << 6, - CLASS = 1 << 7, - INTERFACE = 1 << 8, - ANNOTATION = 1 << 9, - GLOBAL_VARIABLE = 1 << 10, - FIELD = 1 << 11, - FUNCTION = 1 << 12, - METHOD = 1 << 13, - ENUM = 1 << 14, - ENUM_CONSTANT = 1 << 15, - TYPEDEF = 1 << 16, - TYPE_PARAMETER = 1 << 17, - FILE = 1 << 18, - MACRO = 1 << 19, - UNION = 1 << 20 - }; +/** + * Enum providing all possible values for kinds of nodes that can be stored to the Sourcetrail database. + */ +enum class NodeKind : int +{ + UNKNOWN = 1 << 0, + TYPE = 1 << 1, + BUILTIN_TYPE = 1 << 2, + MODULE = 1 << 3, + NAMESPACE = 1 << 4, + PACKAGE = 1 << 5, + STRUCT = 1 << 6, + CLASS = 1 << 7, + INTERFACE = 1 << 8, + ANNOTATION = 1 << 9, + GLOBAL_VARIABLE = 1 << 10, + FIELD = 1 << 11, + FUNCTION = 1 << 12, + METHOD = 1 << 13, + ENUM = 1 << 14, + ENUM_CONSTANT = 1 << 15, + TYPEDEF = 1 << 16, + TYPE_PARAMETER = 1 << 17, + FILE = 1 << 18, + MACRO = 1 << 19, + UNION = 1 << 20 +}; - int nodeKindToInt(NodeKind kind); - NodeKind intToNodeKind(int i); -} +int nodeKindToInt(NodeKind kind); +NodeKind intToNodeKind(int i); +} // namespace sourcetrail -#endif // SOURCETRAIL_NODE_KIND_H +#endif // SOURCETRAIL_NODE_KIND_H diff --git a/core/include/ReferenceKind.h b/core/include/ReferenceKind.h index 328f9ab..4e36002 100644 --- a/core/include/ReferenceKind.h +++ b/core/include/ReferenceKind.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_REFERENCE_KIND_H #define SOURCETRAIL_REFERENCE_KIND_H @@ -21,25 +21,25 @@ namespace sourcetrail { - /** - * Enum providing all possible values for kinds of references that can be recorded using the SourcetrailDBWriter interface. - */ - enum class ReferenceKind - { - TYPE_USAGE, - USAGE, - CALL, - INHERITANCE, - OVERRIDE, - TYPE_ARGUMENT, - TEMPLATE_SPECIALIZATION, - INCLUDE, - IMPORT, - MACRO_USAGE, - ANNOTATION_USAGE - }; +/** + * Enum providing all possible values for kinds of references that can be recorded using the SourcetrailDBWriter interface. + */ +enum class ReferenceKind +{ + TYPE_USAGE, + USAGE, + CALL, + INHERITANCE, + OVERRIDE, + TYPE_ARGUMENT, + TEMPLATE_SPECIALIZATION, + INCLUDE, + IMPORT, + MACRO_USAGE, + ANNOTATION_USAGE +}; - EdgeKind referenceKindToEdgeKind(ReferenceKind kind); -} +EdgeKind referenceKindToEdgeKind(ReferenceKind kind); +} // namespace sourcetrail -#endif // SOURCETRAIL_REFERENCE_KIND_H +#endif // SOURCETRAIL_REFERENCE_KIND_H diff --git a/core/include/SourceRange.h b/core/include/SourceRange.h index 51e2e44..932ade0 100644 --- a/core/include/SourceRange.h +++ b/core/include/SourceRange.h @@ -1,38 +1,38 @@ /* -* 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. -*/ + * 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_SOURCE_RANGE_H #define SOURCETRAIL_SOURCE_RANGE_H 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! - * note: The SourceRange includes both, the start and the end column number. - */ - struct SourceRange - { - int fileId; - int startLine; - int startColumn; - int endLine; - int endColumn; - }; -} +/** + * 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! + * note: The SourceRange includes both, the start and the end column number. + */ +struct SourceRange +{ + int fileId; + int startLine; + int startColumn; + int endLine; + int endColumn; +}; +} // namespace sourcetrail -#endif // SOURCETRAIL_SOURCE_RANGE_H +#endif // SOURCETRAIL_SOURCE_RANGE_H diff --git a/core/include/SourcetrailDBWriter.h b/core/include/SourcetrailDBWriter.h index 46edcaf..2c4ea6d 100644 --- a/core/include/SourcetrailDBWriter.h +++ b/core/include/SourcetrailDBWriter.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_SRCTRLDB_WRITER_H #define SOURCETRAIL_SRCTRLDB_WRITER_H @@ -31,494 +31,494 @@ namespace sourcetrail { - class DatabaseStorage; +class DatabaseStorage; + +/** + * SourcetrailDBWriter + * + * This class is the main interface for writing data to a Sourcetrail project database. + * It be used to manage a Sourcetrail database file and to write information to it. + * + * The following code snippet illustrates a very basic usage of the SourcetrailDBWriter class: + * + * sourcetrail::SourcetrailDBWriter writer; + * writer.open("MyProject.srctrldb"); + * writer.recordSymbol({ "::",{ { "void", "foo", "()" } } }); + * writer.close(); + */ +class SourcetrailDBWriter +{ +public: + SourcetrailDBWriter(); + ~SourcetrailDBWriter(); /** - * SourcetrailDBWriter + * Provides the version of the SourcetrailDB Core as string with format "vXX.dbYY.pZZ" * - * This class is the main interface for writing data to a Sourcetrail project database. - * It be used to manage a Sourcetrail database file and to write information to it. + * - XX: interface version. This version increases on every change that breaks backwards + * compatibility. + * - YY: Sourcetrail database version. This version needs to match the database version + * of the used Sourcetrail instance. You can find the database version of Sourcetrail + * in its About dialog. + * - ZZ: patch number of the build. It will increase with every release that publishes + * bugfixes and features that don't break any compatibility. * - * The following code snippet illustrates a very basic usage of the SourcetrailDBWriter class: - * - * sourcetrail::SourcetrailDBWriter writer; - * writer.open("MyProject.srctrldb"); - * writer.recordSymbol({ "::",{ { "void", "foo", "()" } } }); - * writer.close(); + * return: version string */ - class SourcetrailDBWriter - { - public: - SourcetrailDBWriter(); - ~SourcetrailDBWriter(); + std::string getVersionString() const; - /** - * Provides the version of the SourcetrailDB Core as string with format "vXX.dbYY.pZZ" - * - * - XX: interface version. This version increases on every change that breaks backwards - * compatibility. - * - YY: Sourcetrail database version. This version needs to match the database version - * of the used Sourcetrail instance. You can find the database version of Sourcetrail - * in its About dialog. - * - ZZ: patch number of the build. It will increase with every release that publishes - * bugfixes and features that don't break any compatibility. - * - * return: version string - */ - std::string getVersionString() const; + /** + * Provides the supported database version as integer + * + * return: supported database version + * + * see: getVersionString() for details + */ + int getSupportedDatabaseVersion() const; - /** - * Provides the supported database version as integer - * - * return: supported database version - * - * see: getVersionString() for details - */ - int getSupportedDatabaseVersion() const; + /** + * Provides the last error that occurred while using the SourcetrailDBWriter + * + * The last error is empty if no error occurred since instantiation of the class or since the + * error has last been cleared. + * + * return: error message of last error that occured + * + * see: clearLastError() + */ + const std::string& getLastError() const; - /** - * Provides the last error that occurred while using the SourcetrailDBWriter - * - * The last error is empty if no error occurred since instantiation of the class or since the - * error has last been cleared. - * - * return: error message of last error that occured - * - * see: clearLastError() - */ - const std::string& getLastError() const; + /** + * Modifies the stored error message + * + * param: error - the new error message + * + * see: getLastError() + */ + void setLastError(const std::string& error) const; - /** - * Modifies the stored error message - * - * param: error - the new error message - * - * see: getLastError() - */ - void setLastError(const std::string& error) const; + /** + * Clears the stored error message + * + * see: getLastError() + */ + void clearLastError(); - /** - * Clears the stored error message - * - * see: getLastError() - */ - void clearLastError(); + /** + * Opens a Sourcetrail database + * + * Call this method to open a Sourcetrail database file. If the database does not have any + * related .srctrlprj project file, a minimal project file will be created that allows for + * opening the database with Sourcetrail. + * + * param: databaseFilePath - absolute file path of the database file, including file extension + * + * return: true if successful. false on failure. getLastError() provides the error message. + */ + bool open(const std::string& databaseFilePath); - /** - * Opens a Sourcetrail database - * - * Call this method to open a Sourcetrail database file. If the database does not have any - * related .srctrlprj project file, a minimal project file will be created that allows for - * opening the database with Sourcetrail. - * - * param: databaseFilePath - absolute file path of the database file, including file extension - * - * return: true if successful. false on failure. getLastError() provides the error message. - */ - bool open(const std::string& databaseFilePath); + /** + * Closes the currently open Sourcetrail database + * + * return: Returns true if the operation was successful. Otherwise false is returned and getLastError() + * can be checked for more detailed information. + * + * see: open(const std::string& databaseFilePath) + */ + bool close(); - /** - * Closes the currently open Sourcetrail database - * - * return: Returns true if the operation was successful. Otherwise false is returned and getLastError() - * can be checked for more detailed information. - * - * see: open(const std::string& databaseFilePath) - */ - bool close(); + /** + * Clears the currently open Sourcetrail database + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: open(const std::string& databaseFilePath) + */ + bool clear(); - /** - * Clears the currently open Sourcetrail database - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: open(const std::string& databaseFilePath) - */ - bool clear(); + /** + * Checks if the currently open database file contains any data + * + * return: true after opening a non-existing database file or clearing the open database + */ + bool isEmpty() const; - /** - * Checks if the currently open database file contains any data - * - * return: true after opening a non-existing database file or clearing the open database - */ - bool isEmpty() const; + /** + * Checks if the currently open database is compatible with the SourcetrailDBWriter version. + * + * return: true for an empty database or a database that has been created with the same + * database version. + * + * see: getSupportedDatabaseVersion() + * see: getLoadedDatabaseVersion() + */ + bool isCompatible() const; - /** - * Checks if the currently open database is compatible with the SourcetrailDBWriter version. - * - * return: true for an empty database or a database that has been created with the same - * database version. - * - * see: getSupportedDatabaseVersion() - * see: getLoadedDatabaseVersion() - */ - bool isCompatible() const; + /** + * Provides the database version of the loaded database file as an integer + * + * return: database version of loaded database + */ + int getLoadedDatabaseVersion() const; - /** - * Provides the database version of the loaded database file as an integer - * - * return: database version of loaded database - */ - int getLoadedDatabaseVersion() const; + /** + * Starts a database transaction on the currently open database + * + * Apart from allowing to restore the database to the state it was in before this method has + * been called, wrapping multiple calls that record data with one transaction significantly + * increases the performance of these database operations. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: commitTransaction() + * see: rollbackTransaction() + */ + bool beginTransaction(); - /** - * Starts a database transaction on the currently open database - * - * Apart from allowing to restore the database to the state it was in before this method has - * been called, wrapping multiple calls that record data with one transaction significantly - * increases the performance of these database operations. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: commitTransaction() - * see: rollbackTransaction() - */ - bool beginTransaction(); + /** + * Ends the current transaction and writes all of its changes persistently to the database + * + * return: true if successful. false on failure. getLastError() provides the error message. + */ + bool commitTransaction(); - /** - * Ends the current transaction and writes all of its changes persistently to the database - * - * return: true if successful. false on failure. getLastError() provides the error message. - */ - bool commitTransaction(); + /** + * Reverts the database to the state it was in before the current transaction was started + * + * return: true if successful. false on failure. getLastError() provides the error message. + */ + bool rollbackTransaction(); - /** - * Reverts the database to the state it was in before the current transaction was started - * - * return: true if successful. false on failure. getLastError() provides the error message. - */ - bool rollbackTransaction(); + /** + * Reduces the on disk memory consumption of the open database to a minimum + * + * return: true if successful. false on failure. getLastError() provides the error message. + */ + bool optimizeDatabaseMemory(); - /** - * Reduces the on disk memory consumption of the open database to a minimum - * - * return: true if successful. false on failure. getLastError() provides the error message. - */ - bool optimizeDatabaseMemory(); + /** + * Stores a symbol to the database + * + * note: Calling this method multiple times with the same input on the same Sourcetrail + * database will always return the same id. + * + * param: nameHierarchy - the name of the symbol to store. + * + * return: symbolId - integer id of the stored symbol. 0 on failure. getLastError() + * provides the error message. + * + * see: NameHierarchy + */ + int recordSymbol(const NameHierarchy& nameHierarchy); - /** - * Stores a symbol to the database - * - * note: Calling this method multiple times with the same input on the same Sourcetrail - * database will always return the same id. - * - * param: nameHierarchy - the name of the symbol to store. - * - * return: symbolId - integer id of the stored symbol. 0 on failure. getLastError() - * provides the error message. - * - * see: NameHierarchy - */ - int recordSymbol(const NameHierarchy& nameHierarchy); + /** + * Stores a definition kind for a specific symbol to the database + * + * Calling this method allows to store a DefinitionKind (e.g. explicitly defined, implicitly + * defined) for a symbol referenced by id to the database. Calling this method with the same + * symbolId multiple times overwrites the symbol's previously recorded DefinitionKind. If no + * DefinitionKind is recorded for a symbol, Sourcetrail treats this symbol as "non-indexed". + * This may be desired while recording a reference to a symbol with a definition that is located + * outside of the indexed source files. + * + * param: symbolId - the id of the symbol for which a DefinitionKind shall be recorded. + * param: definitionKind - the DefinitionKind that shall be recorded for the respective symbol. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: DefinitionKind + */ + bool recordSymbolDefinitionKind(int symbolId, DefinitionKind definitionKind); - /** - * Stores a definition kind for a specific symbol to the database - * - * Calling this method allows to store a DefinitionKind (e.g. explicitly defined, implicitly - * defined) for a symbol referenced by id to the database. Calling this method with the same - * symbolId multiple times overwrites the symbol's previously recorded DefinitionKind. If no - * DefinitionKind is recorded for a symbol, Sourcetrail treats this symbol as "non-indexed". - * This may be desired while recording a reference to a symbol with a definition that is located - * outside of the indexed source files. - * - * param: symbolId - the id of the symbol for which a DefinitionKind shall be recorded. - * param: definitionKind - the DefinitionKind that shall be recorded for the respective symbol. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: DefinitionKind - */ - bool recordSymbolDefinitionKind(int symbolId, DefinitionKind definitionKind); + /** + * Stores a symbol kind for a specific symbol to the database + * + * Calling this method allows to store a SymbolKind (e.g. "class", "function", etc.) for a + * symbol referenced by id to the database. Calling this method with the same symbolId multiple + * times overwrites the symbol's previously recorded SymbolKind. If no SymbolKind is recorded + * for a symbol, Sourcetrail displays the type of this symbol as "symbol". + * + * param: symbolId - the id of the symbol for which a SymbolKind shall be recorded. + * param: symbolKind - the SymbolKind that shall be recorded for the respective symbol. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: SymbolKind + */ + bool recordSymbolKind(int symbolId, SymbolKind symbolKind); - /** - * Stores a symbol kind for a specific symbol to the database - * - * Calling this method allows to store a SymbolKind (e.g. "class", "function", etc.) for a - * symbol referenced by id to the database. Calling this method with the same symbolId multiple - * times overwrites the symbol's previously recorded SymbolKind. If no SymbolKind is recorded - * for a symbol, Sourcetrail displays the type of this symbol as "symbol". - * - * param: symbolId - the id of the symbol for which a SymbolKind shall be recorded. - * param: symbolKind - the SymbolKind that shall be recorded for the respective symbol. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: SymbolKind - */ - bool recordSymbolKind(int symbolId, SymbolKind symbolKind); + /** + * Stores a location for a specific symbol to the database + * + * This method allows to store a location for a symbol referenced by id to the database. + * Calling this method with the same symbolId multiple times adds multiple locations for the + * respective symbol. The stored location will be clickable and displayable. When clicked + * Sourcetrail will activate the symbol with the specified id. When the symbol with the specified + * id is activated, this location will be displayed and highlighted by Sourcetrail. + * + * param: symbolId - the id of the symbol for which a location shall be recorded. + * param: location - the SourceRange that shall be recorded as location for the respective symbol. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: SourceRange + */ + bool recordSymbolLocation(int symbolId, const SourceRange& location); - /** - * Stores a location for a specific symbol to the database - * - * This method allows to store a location for a symbol referenced by id to the database. - * Calling this method with the same symbolId multiple times adds multiple locations for the - * respective symbol. The stored location will be clickable and displayable. When clicked - * Sourcetrail will activate the symbol with the specified id. When the symbol with the specified - * id is activated, this location will be displayed and highlighted by Sourcetrail. - * - * param: symbolId - the id of the symbol for which a location shall be recorded. - * param: location - the SourceRange that shall be recorded as location for the respective symbol. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: SourceRange - */ - bool recordSymbolLocation(int symbolId, const SourceRange& location); + /** + * Stores a scope location for a specific symbol to the database + * + * This method allows to store a scope location for a symbol referenced by id to the database. + * Calling this method with the same symbolId multiple times adds multiple scope locations for the + * respective symbol. The stored location will only be displayable and not clickable. When the + * symbol with the specified id is activated, this location will be fully displayed but not + * highlighted by Sourcetrail. + * + * param: symbolId - the id of the symbol for which a scope location shall be recorded. + * param: location - the SourceRange that shall be recorded as scope location for the respective + * symbol. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: SourceRange + */ + bool recordSymbolScopeLocation(int symbolId, const SourceRange& location); - /** - * Stores a scope location for a specific symbol to the database - * - * This method allows to store a scope location for a symbol referenced by id to the database. - * Calling this method with the same symbolId multiple times adds multiple scope locations for the - * respective symbol. The stored location will only be displayable and not clickable. When the - * symbol with the specified id is activated, this location will be fully displayed but not - * highlighted by Sourcetrail. - * - * param: symbolId - the id of the symbol for which a scope location shall be recorded. - * param: location - the SourceRange that shall be recorded as scope location for the respective - * symbol. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: SourceRange - */ - bool recordSymbolScopeLocation(int symbolId, const SourceRange& location); + /** + * Stores a signature location for a specific symbol to the database + * + * This method allows to store a signature location for a symbol referenced by id to the + * database. If a signature location is recorded for a symbol, Sourcetrail will display the + * respective source code in a tooltip whenever the symbol with the referenced id or any of + * that symbol's locations gets hovered. + * If no signature location is recorded for a symbol, Sourcetrail will show its name hierarchy. + * + * note: Calling this method with the same symbolId multiple times adds multiple signature + * locations for the respective symbol. It is not guaranteed which one will be used, so it is + * advised to call it only once per symbol. + * + * param: symbolId - the id of the symbol for which a signature location shall be recorded. + * param: location - the SourceRange that shall be recorded as signature location for the + * respective symbol. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: SourceRange + */ + bool recordSymbolSignatureLocation(int symbolId, const SourceRange& location); - /** - * Stores a signature location for a specific symbol to the database - * - * This method allows to store a signature location for a symbol referenced by id to the - * database. If a signature location is recorded for a symbol, Sourcetrail will display the - * respective source code in a tooltip whenever the symbol with the referenced id or any of - * that symbol's locations gets hovered. - * If no signature location is recorded for a symbol, Sourcetrail will show its name hierarchy. - * - * note: Calling this method with the same symbolId multiple times adds multiple signature - * locations for the respective symbol. It is not guaranteed which one will be used, so it is - * advised to call it only once per symbol. - * - * param: symbolId - the id of the symbol for which a signature location shall be recorded. - * param: location - the SourceRange that shall be recorded as signature location for the - * respective symbol. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: SourceRange - */ - bool recordSymbolSignatureLocation(int symbolId, const SourceRange& location); + /** + * Stores a reference between two symbols to the database + * + * This method allows to store the information of symbols referencing one another to the database. + * For each recorded reference Sourcetrail's graph view will display an edge that originates at + * the reference's recorded context symbol and points to the recorded referenced symbol. The + * recorded ReferenceKind is used to determine the type of the displayed edge and to generate a + * description in the hover tooltip of the edge. + * + * note: Calling this method multiple times with the same input on the same Sourcetrail database + * will always return the same id. + * + * param: contextSymbolId - the id of the source of the recorded reference edge + * param: referencedSymbolId - the id of the target of the recorded reference edge + * param: referenceKind - kind of the recorded reference edge + * + * return: referenceId - integer id of the stored reference. 0 on failure. getLastError() + * provides the error message. + * + * see: ReferenceKind + */ + int recordReference(int contextSymbolId, int referencedSymbolId, ReferenceKind referenceKind); - /** - * Stores a reference between two symbols to the database - * - * This method allows to store the information of symbols referencing one another to the database. - * For each recorded reference Sourcetrail's graph view will display an edge that originates at - * the reference's recorded context symbol and points to the recorded referenced symbol. The - * recorded ReferenceKind is used to determine the type of the displayed edge and to generate a - * description in the hover tooltip of the edge. - * - * note: Calling this method multiple times with the same input on the same Sourcetrail database - * will always return the same id. - * - * param: contextSymbolId - the id of the source of the recorded reference edge - * param: referencedSymbolId - the id of the target of the recorded reference edge - * param: referenceKind - kind of the recorded reference edge - * - * return: referenceId - integer id of the stored reference. 0 on failure. getLastError() - * provides the error message. - * - * see: ReferenceKind - */ - int recordReference(int contextSymbolId, int referencedSymbolId, ReferenceKind referenceKind); + /** + * Stores a location for a specific reference to the database + * + * This method allows to store a location for a reference to the database. + * Calling this method with the same referenceId multiple times adds multiple locations for the + * respective reference. The stored location will be clickable and displayable. When the reference + * location is clicked Sourcetrail will activate the symbol referenced by the respective reference. + * When the reference with the specified id is activated, this location will be displayed and + * highlighted by Sourcetrail. + * + * param: referenceId - the id of the reference for which a location shall be recorded. + * param: location - the SourceRange that shall be recorded as location for the respective + * reference. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: SourceRange + */ + bool recordReferenceLocation(int referenceId, const SourceRange& location); - /** - * Stores a location for a specific reference to the database - * - * This method allows to store a location for a reference to the database. - * Calling this method with the same referenceId multiple times adds multiple locations for the - * respective reference. The stored location will be clickable and displayable. When the reference - * location is clicked Sourcetrail will activate the symbol referenced by the respective reference. - * When the reference with the specified id is activated, this location will be displayed and - * highlighted by Sourcetrail. - * - * param: referenceId - the id of the reference for which a location shall be recorded. - * param: location - the SourceRange that shall be recorded as location for the respective - * reference. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: SourceRange - */ - 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 recordReferenceIsAmbiguous(int referenceId); - /** - * 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 recordReferenceIsAmbiguous(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 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 + * + * This method allows to store a location where a specific symbol is used as a qualifier to the + * database. Calling this method with the same referencedSymbolId multiple times adds multiple locations + * for the respective reference. The stored location will be clickable but not displayable: When the + * reference location is clicked Sourcetrail will activate the symbol referenced by the respective + * reference. When the symbol with the specified id is activated, this location will NOT be displayed and + * highlighted by Sourcetrail. + * + * param: referencedSymbolId - the id of the symbol that is used as qualifier at the current location. + * param: location - the SourceRange that shall be recorded as location for the symbol's occurrence as + * qualifier + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: SourceRange + */ + bool recordQualifierLocation(int referencedSymbolId, const SourceRange& location); - /** - * Stores a location for the usage of a symbol's name as qualifier to the database - * - * This method allows to store a location where a specific symbol is used as a qualifier to the - * database. Calling this method with the same referencedSymbolId multiple times adds multiple locations - * for the respective reference. The stored location will be clickable but not displayable: When the - * reference location is clicked Sourcetrail will activate the symbol referenced by the respective - * reference. When the symbol with the specified id is activated, this location will NOT be displayed and - * highlighted by Sourcetrail. - * - * param: referencedSymbolId - the id of the symbol that is used as qualifier at the current location. - * param: location - the SourceRange that shall be recorded as location for the symbol's occurrence as - * qualifier - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: SourceRange - */ - bool recordQualifierLocation(int referencedSymbolId, const SourceRange& location); + /** + * Stores a file to the database + * + * note: Calling this method multiple times with the same input on the same Sourcetrail database + * will always return the same id. + * + * param: filePath - the absolute path to the file to store. + * + * return: fileId - integer id of the stored file. 0 on failure. getLastError() provides the + * error message. + */ + int recordFile(const std::string& filePath); - /** - * Stores a file to the database - * - * note: Calling this method multiple times with the same input on the same Sourcetrail database - * will always return the same id. - * - * param: filePath - the absolute path to the file to store. - * - * return: fileId - integer id of the stored file. 0 on failure. getLastError() provides the - * error message. - */ - int recordFile(const std::string& filePath); + /** + * Stores language information for a specific file to the database + * + * This method allows to store language information for a file to the database. Language information + * is passed and stored as string (e.g. "cpp", "java", etc.) and allows Sourcetrail to pick the + * correct syntax highlighting rules when displaying the respective source file. + * + * param: fileId - the id of the file for which language information shall be recorded. + * param: languageIdentifier - a string that denotes the programming language the respective file + * is written in. + * + * return: true if successful. false on failure. getLastError() provides the error message. + */ + bool recordFileLanguage(int fileId, const std::string& languageIdentifier); - /** - * Stores language information for a specific file to the database - * - * This method allows to store language information for a file to the database. Language information - * is passed and stored as string (e.g. "cpp", "java", etc.) and allows Sourcetrail to pick the - * correct syntax highlighting rules when displaying the respective source file. - * - * param: fileId - the id of the file for which language information shall be recorded. - * param: languageIdentifier - a string that denotes the programming language the respective file - * is written in. - * - * return: true if successful. false on failure. getLastError() provides the error message. - */ - bool recordFileLanguage(int fileId, const std::string& languageIdentifier); + /** + * Stores a local symbol to the database + * + * note: Calling this method multiple times with the same input on the same Sourcetrail database + * will always return the same id. + * + * param: name - a name that is unique for this local symbol (e.g. the string encoded location + * of the local symbol's definition). + * + * return: localSymbolId - integer id of the stored local symbol. 0 on failure. getLastError() + * provides the error message. + */ + int recordLocalSymbol(const std::string& name); - /** - * Stores a local symbol to the database - * - * note: Calling this method multiple times with the same input on the same Sourcetrail database - * will always return the same id. - * - * param: name - a name that is unique for this local symbol (e.g. the string encoded location - * of the local symbol's definition). - * - * return: localSymbolId - integer id of the stored local symbol. 0 on failure. getLastError() - * provides the error message. - */ - int recordLocalSymbol(const std::string& name); + /** + * Stores a location for a specific local symbol to the database + * + * This method allows to store a location for a local symbol symbol referenced by id to the database. + * Calling this method with the same symbolId multiple times adds multiple locations for the + * respective local symbol. The stored location will be clickable and displayable. When clicked + * Sourcetrail will activate this and all other local symbol locations that share the same local + * symbol id. + * + * param: localSymbolId - the id of the local symbol for which a location shall be recorded. + * param: location - the SourceRange that shall be recorded as location for the respective + * local symbol. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: SourceRange + */ + bool recordLocalSymbolLocation(int localSymbolId, const SourceRange& location); - /** - * Stores a location for a specific local symbol to the database - * - * This method allows to store a location for a local symbol symbol referenced by id to the database. - * Calling this method with the same symbolId multiple times adds multiple locations for the - * respective local symbol. The stored location will be clickable and displayable. When clicked - * Sourcetrail will activate this and all other local symbol locations that share the same local - * symbol id. - * - * param: localSymbolId - the id of the local symbol for which a location shall be recorded. - * param: location - the SourceRange that shall be recorded as location for the respective - * local symbol. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: SourceRange - */ - bool recordLocalSymbolLocation(int localSymbolId, const SourceRange& location); + /** + * Stores an atomic SourceRange to the database + * + * This method allows to store an atomic SourceRange to the database. These ranges will + * be used by Sourcetrail to prevent the code view from displaying only a part of the range. Thus, + * if any line that lies within one of the project's atomic ranges is dispayed, the remaining + * lines will be displayed as well. This may be useful for dealing with multi-line comments or + * multi-line strings. + * + * param: sourceRange - the SourceRange to record. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: SourceRange + */ + bool recordAtomicSourceRange(const SourceRange& sourceRange); - /** - * Stores an atomic SourceRange to the database - * - * This method allows to store an atomic SourceRange to the database. These ranges will - * be used by Sourcetrail to prevent the code view from displaying only a part of the range. Thus, - * if any line that lies within one of the project's atomic ranges is dispayed, the remaining - * lines will be displayed as well. This may be useful for dealing with multi-line comments or - * multi-line strings. - * - * param: sourceRange - the SourceRange to record. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: SourceRange - */ - bool recordAtomicSourceRange(const SourceRange& sourceRange); + /** + * Stores an indexing error to the database + * + * This method allows to store an indexing error to the database. Errors will be shown by + * Sourcetrail's error view. When clicking the error in Sourcetrail's error list, the code view + * will display this location. + * + * param: message - an error message that will be displayed in Sourcetrail's error list. + * param: fatal - boolean fatal indicates whether parsing/indexing was aborted at this point. + * param: location - the SourceRange of the error encountered. + * + * return: true if successful. false on failure. getLastError() provides the error message. + * + * see: SourceRange + */ + bool recordError(const std::string& message, bool fatal, const SourceRange& location); - /** - * Stores an indexing error to the database - * - * This method allows to store an indexing error to the database. Errors will be shown by - * Sourcetrail's error view. When clicking the error in Sourcetrail's error list, the code view - * will display this location. - * - * param: message - an error message that will be displayed in Sourcetrail's error list. - * param: fatal - boolean fatal indicates whether parsing/indexing was aborted at this point. - * param: location - the SourceRange of the error encountered. - * - * return: true if successful. false on failure. getLastError() provides the error message. - * - * see: SourceRange - */ - bool recordError(const std::string& message, bool fatal, const SourceRange& location); +private: + void openDatabase(); + void closeDatabase(); + void setupDatabaseTables(); + void clearDatabaseTables(); + void createOrResetProjectFile(); + void updateProjectSettingsText(); + 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); + void addElementComponent(int elementId, ElementComponentKind kind, const std::string& data); - private: - void openDatabase(); - void closeDatabase(); - void setupDatabaseTables(); - void clearDatabaseTables(); - void createOrResetProjectFile(); - void updateProjectSettingsText(); - 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); - void addElementComponent(int elementId, ElementComponentKind kind, const std::string& data); + std::string m_projectFilePath; + std::string m_databaseFilePath; + std::unique_ptr m_storage; + mutable std::string m_lastError; +}; +} // namespace sourcetrail - std::string m_projectFilePath; - std::string m_databaseFilePath; - std::unique_ptr m_storage; - mutable std::string m_lastError; - }; -} - -#endif // SOURCETRAIL_SRCTRLDB_WRITER_H +#endif // SOURCETRAIL_SRCTRLDB_WRITER_H diff --git a/core/include/SourcetrailException.h b/core/include/SourcetrailException.h index f99426b..7d7ddc7 100644 --- a/core/include/SourcetrailException.h +++ b/core/include/SourcetrailException.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_SOURCETRAIL_EXCEPTION_H #define SOURCETRAIL_SOURCETRAIL_EXCEPTION_H @@ -21,19 +21,22 @@ namespace sourcetrail { - /** - * Exception type that is thrown by the SourcetrailDBWriter API. - */ - class SourcetrailException +/** + * 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 { - public: - SourcetrailException(std::string message) : m_message(message) {} - virtual ~SourcetrailException() = default; - std::string getMessage() const { return m_message; } + return m_message; + } - private: - std::string m_message; - }; -} +private: + std::string m_message; +}; +} // namespace sourcetrail -#endif // SOURCETRAIL_SOURCETRAIL_EXCEPTION_H +#endif // SOURCETRAIL_SOURCETRAIL_EXCEPTION_H diff --git a/core/include/StorageEdge.h b/core/include/StorageEdge.h index 583f448..9ab7f14 100644 --- a/core/include/StorageEdge.h +++ b/core/include/StorageEdge.h @@ -1,62 +1,48 @@ /* -* 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. -*/ + * 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_EDGE_H #define SOURCETRAIL_STORAGE_EDGE_H namespace sourcetrail { - struct StorageEdgeData +struct StorageEdgeData +{ + StorageEdgeData(): sourceNodeId(0), targetNodeId(0), edgeKind(0) {} + + StorageEdgeData(int sourceNodeId, int targetNodeId, int edgeKind) + : sourceNodeId(sourceNodeId), targetNodeId(targetNodeId), edgeKind(edgeKind) { - StorageEdgeData() - : sourceNodeId(0) - , targetNodeId(0) - , edgeKind(0) - {} + } - StorageEdgeData(int sourceNodeId, int targetNodeId, int edgeKind) - : sourceNodeId(sourceNodeId) - , targetNodeId(targetNodeId) - , edgeKind(edgeKind) - {} + int sourceNodeId; + int targetNodeId; + int edgeKind; +}; - int sourceNodeId; - int targetNodeId; - int edgeKind; - }; +struct StorageEdge: public StorageEdgeData +{ + StorageEdge(): StorageEdgeData(), id(0) {} - struct StorageEdge : public StorageEdgeData - { - StorageEdge() - : StorageEdgeData() - , id(0) - {} + StorageEdge(int id, const StorageEdgeData& data): StorageEdgeData(data), id(id) {} - StorageEdge(int id, const StorageEdgeData& data) - : StorageEdgeData(data) - , id(id) - {} + StorageEdge(int id, int sourceNodeId, int targetNodeId, int edgeKind): StorageEdgeData(sourceNodeId, targetNodeId, edgeKind), id(id) {} - StorageEdge(int id, int sourceNodeId, int targetNodeId, int edgeKind) - : StorageEdgeData(sourceNodeId, targetNodeId, edgeKind) - , id(id) - {} + int id; +}; +} // namespace sourcetrail - int id; - }; -} - -#endif // SOURCETRAIL_STORAGE_EDGE_H +#endif // SOURCETRAIL_STORAGE_EDGE_H diff --git a/core/include/StorageElementComponent.h b/core/include/StorageElementComponent.h index 983a292..a75fa4c 100644 --- a/core/include/StorageElementComponent.h +++ b/core/include/StorageElementComponent.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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 @@ -21,44 +21,33 @@ namespace sourcetrail { - struct StorageElementComponentData +struct StorageElementComponentData +{ + StorageElementComponentData(): elementId(0), componentKind(0), data("") {} + + StorageElementComponentData(int elementId, int componentKind, std::string data) + : elementId(elementId), componentKind(componentKind), data(std::move(data)) { - 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; +}; - int elementId; - int componentKind; - std::string data; - }; +struct StorageElementComponent: public StorageElementComponentData +{ + StorageElementComponent(): StorageElementComponentData(), id(0) {} - struct StorageElementComponent : public StorageElementComponentData + 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) { - StorageElementComponent() - : StorageElementComponentData() - , id(0) - {} + } - StorageElementComponent(int id, const StorageElementComponentData& data) - : StorageElementComponentData(data) - , id(id) - {} + int id; +}; +} // namespace sourcetrail - StorageElementComponent(int id, int elementId, int componentKind, std::string data) - : StorageElementComponentData(elementId, componentKind, data) - , id(id) - {} - - int id; - }; -} - -#endif // SOURCETRAIL_STORAGE_ELEMENT_COMPONENT_H +#endif // SOURCETRAIL_STORAGE_ELEMENT_COMPONENT_H diff --git a/core/include/StorageError.h b/core/include/StorageError.h index 23e1e2e..695c9d1 100644 --- a/core/include/StorageError.h +++ b/core/include/StorageError.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_ERROR_H #define SOURCETRAIL_STORAGE_ERROR_H @@ -21,64 +21,34 @@ namespace sourcetrail { - struct StorageErrorData +struct StorageErrorData +{ + StorageErrorData(): message(""), translationUnit(""), fatal(0), indexed(0) {} + + StorageErrorData(std::string message, std::string translationUnit, bool fatal, bool indexed) + : message(std::move(message)), translationUnit(std::move(translationUnit)), fatal(fatal), indexed(indexed) { - StorageErrorData() - : message("") - , translationUnit("") - , fatal(0) - , indexed(0) + } - {} + std::string message; + std::string translationUnit; + bool fatal; + bool indexed; +}; - StorageErrorData( - std::string message, - std::string translationUnit, - bool fatal, - bool indexed - ) - : message(std::move(message)) - , translationUnit(std::move(translationUnit)) - , fatal(fatal) - , indexed(indexed) - {} +struct StorageError: public StorageErrorData +{ + StorageError(): StorageErrorData(), id(0) {} - std::string message; - std::string translationUnit; - bool fatal; - bool indexed; - }; + StorageError(int id, const StorageErrorData& data): StorageErrorData(data), id(id) {} - struct StorageError : public StorageErrorData + StorageError(int id, std::string message, std::string translationUnit, bool fatal, bool indexed) + : StorageErrorData(std::move(message), std::move(translationUnit), fatal, indexed), id(id) { - StorageError() - : StorageErrorData() - , id(0) - {} + } - StorageError(int id, const StorageErrorData& data) - : StorageErrorData(data) - , id(id) - {} + int id; +}; +} // namespace sourcetrail - StorageError( - int id, - std::string message, - std::string translationUnit, - bool fatal, - bool indexed - ) - : StorageErrorData( - std::move(message), - std::move(translationUnit), - fatal, - indexed - ) - , id(id) - {} - - int id; - }; -} - -#endif // SOURCETRAIL_STORAGE_ERROR_H +#endif // SOURCETRAIL_STORAGE_ERROR_H diff --git a/core/include/StorageFile.h b/core/include/StorageFile.h index 1ee1585..c144b65 100644 --- a/core/include/StorageFile.h +++ b/core/include/StorageFile.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_FILE_H #define SOURCETRAIL_STORAGE_FILE_H @@ -21,33 +21,27 @@ namespace sourcetrail { - struct StorageFile +struct StorageFile +{ + StorageFile(): id(0), filePath(""), languageIdentifier(""), modificationTime(""), indexed(true), complete(true) {} + + StorageFile(int id, std::string filePath, std::string languageIdentifier, std::string modificationTime, bool indexed, bool complete) + : id(id) + , filePath(std::move(filePath)) + , languageIdentifier(std::move(languageIdentifier)) + , modificationTime(std::move(modificationTime)) + , indexed(indexed) + , complete(complete) { - StorageFile() - : id(0) - , filePath("") - , languageIdentifier("") - , modificationTime("") - , indexed(true) - , complete(true) - {} + } - StorageFile(int id, std::string filePath, std::string languageIdentifier, std::string modificationTime, bool indexed, bool complete) - : id(id) - , filePath(std::move(filePath)) - , languageIdentifier(std::move(languageIdentifier)) - , modificationTime(std::move(modificationTime)) - , indexed(indexed) - , complete(complete) - {} + int id; + std::string filePath; + std::string languageIdentifier; + std::string modificationTime; + bool indexed; + bool complete; +}; +} // namespace sourcetrail - int id; - std::string filePath; - std::string languageIdentifier; - std::string modificationTime; - bool indexed; - bool complete; - }; -} - -#endif // SOURCETRAIL_STORAGE_FILE_H +#endif // SOURCETRAIL_STORAGE_FILE_H diff --git a/core/include/StorageLocalSymbol.h b/core/include/StorageLocalSymbol.h index 1f4134c..6273936 100644 --- a/core/include/StorageLocalSymbol.h +++ b/core/include/StorageLocalSymbol.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_LOCAL_SYMBOL_H #define SOURCETRAIL_STORAGE_LOCAL_SYMBOL_H @@ -21,38 +21,25 @@ namespace sourcetrail { - struct StorageLocalSymbolData - { - StorageLocalSymbolData() - : name("") - {} +struct StorageLocalSymbolData +{ + StorageLocalSymbolData(): name("") {} - StorageLocalSymbolData(std::string name) - : name(std::move(name)) - {} + StorageLocalSymbolData(std::string name): name(std::move(name)) {} - std::string name; - }; + std::string name; +}; - struct StorageLocalSymbol : public StorageLocalSymbolData - { - StorageLocalSymbol() - : StorageLocalSymbolData() - , id(0) - {} +struct StorageLocalSymbol: public StorageLocalSymbolData +{ + StorageLocalSymbol(): StorageLocalSymbolData(), id(0) {} - StorageLocalSymbol(int id, const StorageLocalSymbolData& data) - : StorageLocalSymbolData(data) - , id(id) - {} + StorageLocalSymbol(int id, const StorageLocalSymbolData& data): StorageLocalSymbolData(data), id(id) {} - StorageLocalSymbol(int id, std::string name) - : StorageLocalSymbolData(std::move(name)) - , id(id) - {} + StorageLocalSymbol(int id, std::string name): StorageLocalSymbolData(std::move(name)), id(id) {} - int id; - }; -} + int id; +}; +} // namespace sourcetrail -#endif // SOURCETRAIL_STORAGE_LOCAL_SYMBOL_H +#endif // SOURCETRAIL_STORAGE_LOCAL_SYMBOL_H diff --git a/core/include/StorageNode.h b/core/include/StorageNode.h index 3ba6d76..ba0b093 100644 --- a/core/include/StorageNode.h +++ b/core/include/StorageNode.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_NODE_H #define SOURCETRAIL_STORAGE_NODE_H @@ -21,41 +21,26 @@ namespace sourcetrail { - struct StorageNodeData - { - StorageNodeData() - : nodeKind(0) - , serializedName("") - {} +struct StorageNodeData +{ + StorageNodeData(): nodeKind(0), serializedName("") {} - StorageNodeData(int nodeKind, std::string serializedName) - : nodeKind(nodeKind) - , serializedName(std::move(serializedName)) - {} + StorageNodeData(int nodeKind, std::string serializedName): nodeKind(nodeKind), serializedName(std::move(serializedName)) {} - int nodeKind; - std::string serializedName; - }; + int nodeKind; + std::string serializedName; +}; - struct StorageNode : public StorageNodeData - { - StorageNode() - : StorageNodeData() - , id(0) - {} +struct StorageNode: public StorageNodeData +{ + StorageNode(): StorageNodeData(), id(0) {} - StorageNode(int id, int nodeKind, std::string serializedName) - : StorageNodeData(nodeKind, std::move(serializedName)) - , id(id) - {} + StorageNode(int id, int nodeKind, std::string serializedName): StorageNodeData(nodeKind, std::move(serializedName)), id(id) {} - StorageNode(int id, const StorageNodeData& data) - : StorageNodeData(data) - , id(id) - {} + StorageNode(int id, const StorageNodeData& data): StorageNodeData(data), id(id) {} - int id; - }; -} + int id; +}; +} // namespace sourcetrail -#endif // SOURCETRAIL_STORAGE_NODE_H +#endif // SOURCETRAIL_STORAGE_NODE_H diff --git a/core/include/StorageOccurrence.h b/core/include/StorageOccurrence.h index 6b1f141..6da97ad 100644 --- a/core/include/StorageOccurrence.h +++ b/core/include/StorageOccurrence.h @@ -1,39 +1,33 @@ /* -* 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. -*/ + * 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_OCCURRENCE_H #define SOURCETRAIL_STORAGE_OCCURRENCE_H namespace sourcetrail { - struct StorageOccurrence - { - StorageOccurrence() - : elementId(0) - , sourceLocationId(0) - {} +struct StorageOccurrence +{ + StorageOccurrence(): elementId(0), sourceLocationId(0) {} - StorageOccurrence(int elementId, int sourceLocationId) - : elementId(elementId) - , sourceLocationId(sourceLocationId) - {} + StorageOccurrence(int elementId, int sourceLocationId): elementId(elementId), sourceLocationId(sourceLocationId) {} - int elementId; - int sourceLocationId; - }; -} + int elementId; + int sourceLocationId; +}; +} // namespace sourcetrail -#endif // SOURCETRAIL_STORAGE_OCCURRENCE_H +#endif // SOURCETRAIL_STORAGE_OCCURRENCE_H diff --git a/core/include/StorageSourceLocation.h b/core/include/StorageSourceLocation.h index 8ac58fc..86b6c7f 100644 --- a/core/include/StorageSourceLocation.h +++ b/core/include/StorageSourceLocation.h @@ -1,71 +1,63 @@ /* -* 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. -*/ + * 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_SOURCE_LOCATION_H #define SOURCETRAIL_STORAGE_SOURCE_LOCATION_H namespace sourcetrail { - struct StorageSourceLocationData +struct StorageSourceLocationData +{ + StorageSourceLocationData() + : fileNodeId(0), startLineNumber(-1), startColumnNumber(-1), endLineNumber(-1), endColumnNumber(-1), locationKind(0) { - StorageSourceLocationData() - : fileNodeId(0) - , startLineNumber(-1) - , startColumnNumber(-1) - , endLineNumber(-1) - , endColumnNumber(-1) - , locationKind(0) - {} + } - StorageSourceLocationData(int fileNodeId, int startLineNumber, int startColumnNumber, int endLineNumber, int endColumnNumber, int locationKind) - : fileNodeId(fileNodeId) - , startLineNumber(startLineNumber) - , startColumnNumber(startColumnNumber) - , endLineNumber(endLineNumber) - , endColumnNumber(endColumnNumber) - , locationKind(locationKind) - {} - - int fileNodeId; - int startLineNumber; - int startColumnNumber; - int endLineNumber; - int endColumnNumber; - int locationKind; - }; - - struct StorageSourceLocation : public StorageSourceLocationData + StorageSourceLocationData(int fileNodeId, int startLineNumber, int startColumnNumber, int endLineNumber, int endColumnNumber, int locationKind) + : fileNodeId(fileNodeId) + , startLineNumber(startLineNumber) + , startColumnNumber(startColumnNumber) + , endLineNumber(endLineNumber) + , endColumnNumber(endColumnNumber) + , locationKind(locationKind) { - StorageSourceLocation() - : StorageSourceLocationData() - , id(0) - {} + } - StorageSourceLocation(int id, const StorageSourceLocationData& data) - : StorageSourceLocationData(data) - , id(id) - {} + int fileNodeId; + int startLineNumber; + int startColumnNumber; + int endLineNumber; + int endColumnNumber; + int locationKind; +}; - StorageSourceLocation(int id, int fileNodeId, int startLineNumber, int startColumnNumber, int endLineNumber, int endColumnNumber, int locationKind) - : StorageSourceLocationData(fileNodeId, startLineNumber, startColumnNumber, endLineNumber, endColumnNumber, locationKind) - , id(id) - {} +struct StorageSourceLocation: public StorageSourceLocationData +{ + StorageSourceLocation(): StorageSourceLocationData(), id(0) {} - int id; - }; -} + StorageSourceLocation(int id, const StorageSourceLocationData& data): StorageSourceLocationData(data), id(id) {} -#endif // SOURCETRAIL_STORAGE_SOURCE_LOCATION_H + StorageSourceLocation( + int id, int fileNodeId, int startLineNumber, int startColumnNumber, int endLineNumber, int endColumnNumber, int locationKind) + : StorageSourceLocationData(fileNodeId, startLineNumber, startColumnNumber, endLineNumber, endColumnNumber, locationKind), id(id) + { + } + + int id; +}; +} // namespace sourcetrail + +#endif // SOURCETRAIL_STORAGE_SOURCE_LOCATION_H diff --git a/core/include/StorageSymbol.h b/core/include/StorageSymbol.h index 2d4ced3..bae0238 100644 --- a/core/include/StorageSymbol.h +++ b/core/include/StorageSymbol.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_SYMBOL_H #define SOURCETRAIL_STORAGE_SYMBOL_H @@ -21,21 +21,15 @@ namespace sourcetrail { - struct StorageSymbol - { - StorageSymbol() - : id(0) - , definitionKind(definitionKindToInt(DefinitionKind::EXPLICIT)) - {} +struct StorageSymbol +{ + StorageSymbol(): id(0), definitionKind(definitionKindToInt(DefinitionKind::EXPLICIT)) {} - StorageSymbol(int id, int definitionKind) - : id(id) - , definitionKind(definitionKind) - {} + StorageSymbol(int id, int definitionKind): id(id), definitionKind(definitionKind) {} - int id; - int definitionKind; - }; -} + int id; + int definitionKind; +}; +} // namespace sourcetrail -#endif // SOURCETRAIL_STORAGE_SYMBOL_H +#endif // SOURCETRAIL_STORAGE_SYMBOL_H diff --git a/core/include/SymbolKind.h b/core/include/SymbolKind.h index 24e814e..0f355c1 100644 --- a/core/include/SymbolKind.h +++ b/core/include/SymbolKind.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_SYMBOL_KIND_H #define SOURCETRAIL_SYMBOL_KIND_H @@ -21,33 +21,33 @@ namespace sourcetrail { - /** - * Enum providing all possible values for kinds of symbols that can be recorded using the SourcetrailDBWriter interface. - */ - enum class SymbolKind - { - TYPE, - BUILTIN_TYPE, - MODULE, - NAMESPACE, - PACKAGE, - STRUCT, - CLASS, - INTERFACE, - ANNOTATION, - GLOBAL_VARIABLE, - FIELD, - FUNCTION, - METHOD, - ENUM, - ENUM_CONSTANT, - TYPEDEF, - TYPE_PARAMETER, - MACRO, - UNION - }; +/** + * Enum providing all possible values for kinds of symbols that can be recorded using the SourcetrailDBWriter interface. + */ +enum class SymbolKind +{ + TYPE, + BUILTIN_TYPE, + MODULE, + NAMESPACE, + PACKAGE, + STRUCT, + CLASS, + INTERFACE, + ANNOTATION, + GLOBAL_VARIABLE, + FIELD, + FUNCTION, + METHOD, + ENUM, + ENUM_CONSTANT, + TYPEDEF, + TYPE_PARAMETER, + MACRO, + UNION +}; - NodeKind symbolKindToNodeKind(SymbolKind kind); -} +NodeKind symbolKindToNodeKind(SymbolKind kind); +} // namespace sourcetrail -#endif // SOURCETRAIL_SYMBOL_KIND_H +#endif // SOURCETRAIL_SYMBOL_KIND_H diff --git a/core/include/utility.h b/core/include/utility.h index 16cf772..71ae947 100644 --- a/core/include/utility.h +++ b/core/include/utility.h @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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_UTILITY_H #define SOURCETRAIL_UTILITY_H @@ -22,13 +22,13 @@ namespace sourcetrail { - namespace utility - { - bool getFileExists(const std::string& filePath); - std::string getFileContent(const std::string& filePath); - std::string getDateTimeString(const time_t& time); - int getLineCount(const std::string s); - } -} +namespace utility +{ +bool getFileExists(const std::string& filePath); +std::string getFileContent(const std::string& filePath); +std::string getDateTimeString(const time_t& time); +int getLineCount(const std::string s); +} // namespace utility +} // namespace sourcetrail -#endif // SOURCETRAIL_UTILITY_H +#endif // SOURCETRAIL_UTILITY_H diff --git a/core/src/DatabaseStorage.cpp b/core/src/DatabaseStorage.cpp index c1366c5..e8c942c 100644 --- a/core/src/DatabaseStorage.cpp +++ b/core/src/DatabaseStorage.cpp @@ -1,982 +1,910 @@ /* -* 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. -*/ + * 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 "DatabaseStorage.h" #include +#include "NodeKind.h" #include "SourcetrailException.h" #include "StorageFile.h" #include "StorageNode.h" #include "StorageSymbol.h" -#include "NodeKind.h" #include "utility.h" #include "version.h" namespace sourcetrail { - // --- Public Interface --- +// --- Public Interface --- - int DatabaseStorage::getSupportedDatabaseVersion() +int DatabaseStorage::getSupportedDatabaseVersion() +{ + return DATABASE_VERSION; +} + +std::unique_ptr DatabaseStorage::openDatabase(const std::string& dbFilePath) +{ + try { - return DATABASE_VERSION; + std::unique_ptr storage = std::unique_ptr(new DatabaseStorage()); + storage->m_database.open(dbFilePath.c_str()); + storage->executeStatement("PRAGMA foreign_keys=ON;"); + return std::move(storage); + } + catch (CppSQLite3Exception e) + { + throw SourcetrailException(e.errorMessage()); + } +} + +DatabaseStorage::~DatabaseStorage() +{ + clearPrecompiledStatements(); + + m_database.close(); +} + +void DatabaseStorage::setupDatabase() +{ + executeStatement("PRAGMA foreign_keys=ON;"); + + if (!isCompatible()) + { + throw SourcetrailException("Unable to setup database tables because database is not compatible."); } - std::unique_ptr DatabaseStorage::openDatabase(const std::string& dbFilePath) + setupTables(); + + setupIndices(); + + setupPrecompiledStatements(); + + insertOrUpdateMetaValue("storage_version", std::to_string(getSupportedDatabaseVersion())); +} + +void DatabaseStorage::clearDatabase() +{ + executeStatement("PRAGMA foreign_keys=OFF;"); + + clearPrecompiledStatements(); + + clearTables(); + + setupDatabase(); +} + +void DatabaseStorage::setProjectSettingsText(const std::string& text) +{ + insertOrUpdateMetaValue("project_settings", text); +} + +bool DatabaseStorage::isEmpty() const +{ + const std::string tableName = "meta"; + CppSQLite3Query q = executeQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "';"); + + if (!q.eof()) { - try - { - std::unique_ptr storage = std::unique_ptr(new DatabaseStorage()); - storage->m_database.open(dbFilePath.c_str()); - storage->executeStatement("PRAGMA foreign_keys=ON;"); - return std::move(storage); - } - catch (CppSQLite3Exception e) - { - throw SourcetrailException(e.errorMessage()); - } + return q.getStringField(0, "") == tableName; } - DatabaseStorage::~DatabaseStorage() + return true; +} + +bool DatabaseStorage::isCompatible() const +{ + if (isEmpty()) { - clearPrecompiledStatements(); - - m_database.close(); - } - - void DatabaseStorage::setupDatabase() - { - executeStatement("PRAGMA foreign_keys=ON;"); - - if (!isCompatible()) - { - throw SourcetrailException("Unable to setup database tables because database is not compatible."); - } - - setupTables(); - - setupIndices(); - - setupPrecompiledStatements(); - - insertOrUpdateMetaValue("storage_version", std::to_string(getSupportedDatabaseVersion())); - } - - void DatabaseStorage::clearDatabase() - { - executeStatement("PRAGMA foreign_keys=OFF;"); - - clearPrecompiledStatements(); - - clearTables(); - - setupDatabase(); - } - - void DatabaseStorage::setProjectSettingsText(const std::string& text) - { - insertOrUpdateMetaValue("project_settings", text); - } - - bool DatabaseStorage::isEmpty() const - { - const std::string tableName = "meta"; - CppSQLite3Query q = executeQuery( - "SELECT name FROM sqlite_master WHERE type='table' AND name='" + tableName + "';" - ); - - if (!q.eof()) - { - return q.getStringField(0, "") == tableName; - } - return true; } - bool DatabaseStorage::isCompatible() const - { - if (isEmpty()) - { - return true; - } + return getLoadedDatabaseVersion() == getSupportedDatabaseVersion(); +} - return getLoadedDatabaseVersion() == getSupportedDatabaseVersion(); +int DatabaseStorage::getLoadedDatabaseVersion() const +{ + if (isEmpty()) + { + throw SourcetrailException("Unable to determine version of an empty database."); } - int DatabaseStorage::getLoadedDatabaseVersion() const + CppSQLite3Query q = executeQuery("SELECT value FROM meta WHERE key = 'storage_version';"); + if (!q.eof()) { - if (isEmpty()) - { - throw SourcetrailException("Unable to determine version of an empty database."); - } + return std::stoi(q.getStringField(0, "0")); + } + return 0; +} - CppSQLite3Query q = executeQuery("SELECT value FROM meta WHERE key = 'storage_version';"); +void DatabaseStorage::beginTransaction() +{ + executeStatement("BEGIN TRANSACTION;"); +} + +void DatabaseStorage::commitTransaction() +{ + executeStatement("COMMIT TRANSACTION;"); +} + +void DatabaseStorage::rollbackTransaction() +{ + executeStatement("ROLLBACK TRANSACTION;"); +} + +void DatabaseStorage::optimizeDatabaseMemory() +{ + 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; + + { + m_findNodeStatement.bind(1, storageNodeData.serializedName.c_str()); + CppSQLite3Query q = executeQuery(m_findNodeStatement); if (!q.eof()) { - return std::stoi(q.getStringField(0, "0")); + id = q.getIntField(0, 0); } - return 0; + m_findNodeStatement.reset(); } - void DatabaseStorage::beginTransaction() + // FIXME: update node nodeKind here + + if (id == 0) { - executeStatement("BEGIN TRANSACTION;"); + id = insertElement(); + + m_insertNodeStatement.bind(1, id); + m_insertNodeStatement.bind(2, storageNodeData.nodeKind); + m_insertNodeStatement.bind(3, storageNodeData.serializedName.c_str()); + executeStatement(m_insertNodeStatement); + m_insertNodeStatement.reset(); } - - void DatabaseStorage::commitTransaction() - { - executeStatement("COMMIT TRANSACTION;"); - } - - void DatabaseStorage::rollbackTransaction() - { - executeStatement("ROLLBACK TRANSACTION;"); - } - - void DatabaseStorage::optimizeDatabaseMemory() - { - 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; - - { - m_findNodeStatement.bind(1, storageNodeData.serializedName.c_str()); - CppSQLite3Query q = executeQuery(m_findNodeStatement); - if (!q.eof()) - { - id = q.getIntField(0, 0); - } - m_findNodeStatement.reset(); - } - - // FIXME: update node nodeKind here - - if (id == 0) - { - id = insertElement(); - - m_insertNodeStatement.bind(1, id); - m_insertNodeStatement.bind(2, storageNodeData.nodeKind); - m_insertNodeStatement.bind(3, storageNodeData.serializedName.c_str()); - executeStatement(m_insertNodeStatement); - m_insertNodeStatement.reset(); - } - return id; - } - - void DatabaseStorage::addSymbol(const StorageSymbol& storageSymbol) - { - m_insertSymbolStatement.bind(1, storageSymbol.id); - m_insertSymbolStatement.bind(2, storageSymbol.definitionKind); - executeStatement(m_insertSymbolStatement); - m_insertSymbolStatement.reset(); - } - - void DatabaseStorage::addFile(const StorageFile& storageFile) - { - { - m_findFileStatement.bind(1, storageFile.id); - CppSQLite3Query q = executeQuery(m_findFileStatement); - bool exists = !q.eof(); - m_findFileStatement.reset(); - if (exists) - { - return; - } - } - - std::string content = ""; - if (utility::getFileExists(storageFile.filePath)) - { - content = utility::getFileContent(storageFile.filePath); - } - const int lineCount = utility::getLineCount(content); - - { - m_insertFileStatement.bind(1, storageFile.id); - m_insertFileStatement.bind(2, storageFile.filePath.c_str()); - m_insertFileStatement.bind(3, storageFile.languageIdentifier.c_str()); - m_insertFileStatement.bind(4, storageFile.modificationTime.c_str()); - m_insertFileStatement.bind(5, storageFile.indexed); - m_insertFileStatement.bind(6, storageFile.complete); - m_insertFileStatement.bind(7, lineCount); - executeStatement(m_insertFileStatement); - m_insertFileStatement.reset(); - } - - if (!content.empty()) - { - m_insertFileContentStatement.bind(1, storageFile.id); - m_insertFileContentStatement.bind(2, content.c_str()); - executeStatement(m_insertFileContentStatement); - m_insertFileContentStatement.reset(); - } - } - - int DatabaseStorage::addEdge(const StorageEdgeData& storageEdgeData) - { - int id = 0; - - { - m_findEdgeStatement.bind(1, storageEdgeData.sourceNodeId); - m_findEdgeStatement.bind(2, storageEdgeData.targetNodeId); - m_findEdgeStatement.bind(3, storageEdgeData.edgeKind); - CppSQLite3Query q = executeQuery(m_findEdgeStatement); - if (!q.eof()) - { - id = q.getIntField(0, 0); - } - m_findEdgeStatement.reset(); - } - - if (id == 0) - { - id = insertElement(); - - m_insertEdgeStatement.bind(1, id); - m_insertEdgeStatement.bind(2, storageEdgeData.edgeKind); - m_insertEdgeStatement.bind(3, storageEdgeData.sourceNodeId); - m_insertEdgeStatement.bind(4, storageEdgeData.targetNodeId); - executeStatement(m_insertEdgeStatement); - m_insertEdgeStatement.reset(); - } - return id; - } - - int DatabaseStorage::addLocalSymbol(const StorageLocalSymbolData& storageLocalSymbolData) - { - int id = 0; - - { - m_findLocalSymbolStmt.bind(1, storageLocalSymbolData.name.c_str()); - CppSQLite3Query q = executeQuery(m_findLocalSymbolStmt); - if (!q.eof()) - { - id = q.getIntField(0, 0); - } - m_findLocalSymbolStmt.reset(); - } - - if (id == 0) - { - id = insertElement(); - - m_insertLocalSymbolStmt.bind(1, id); - m_insertLocalSymbolStmt.bind(2, storageLocalSymbolData.name.c_str()); - executeStatement(m_insertLocalSymbolStmt); - m_insertLocalSymbolStmt.reset(); - } - return id; - } - - int DatabaseStorage::addSourceLocation(const StorageSourceLocationData& storageSourceLocationData) - { - int id = 0; - - { - m_findSourceLocationStmt.bind(1, storageSourceLocationData.fileNodeId); - m_findSourceLocationStmt.bind(2, storageSourceLocationData.startLineNumber); - m_findSourceLocationStmt.bind(3, storageSourceLocationData.startColumnNumber); - m_findSourceLocationStmt.bind(4, storageSourceLocationData.endLineNumber); - m_findSourceLocationStmt.bind(5, storageSourceLocationData.endColumnNumber); - m_findSourceLocationStmt.bind(6, storageSourceLocationData.locationKind); - CppSQLite3Query q = executeQuery(m_findSourceLocationStmt); - if (!q.eof()) - { - id = q.getIntField(0, 0); - } - m_findSourceLocationStmt.reset(); - } - - if (id == 0) - { - m_insertSourceLocationStmt.bind(1, storageSourceLocationData.fileNodeId); - m_insertSourceLocationStmt.bind(2, storageSourceLocationData.startLineNumber); - m_insertSourceLocationStmt.bind(3, storageSourceLocationData.startColumnNumber); - m_insertSourceLocationStmt.bind(4, storageSourceLocationData.endLineNumber); - m_insertSourceLocationStmt.bind(5, storageSourceLocationData.endColumnNumber); - m_insertSourceLocationStmt.bind(6, storageSourceLocationData.locationKind); - executeStatement(m_insertSourceLocationStmt); - id = m_database.lastRowId(); - m_insertSourceLocationStmt.reset(); - } - return id; - } - - void DatabaseStorage::addOccurrence(const StorageOccurrence& storageOccurrence) - { - m_insertOccurenceStmt.bind(1, storageOccurrence.elementId); - m_insertOccurenceStmt.bind(2, storageOccurrence.sourceLocationId); - executeStatement(m_insertOccurenceStmt); - m_insertOccurenceStmt.reset(); - } - - int DatabaseStorage::addError(const StorageErrorData& storageErrorData) - { - int id = 0; - { - m_findErrorStatement.bind(1, storageErrorData.message.c_str()); - m_findErrorStatement.bind(2, storageErrorData.fatal); - CppSQLite3Query q = executeQuery(m_findErrorStatement); - if (!q.eof() && q.numFields() > 0) - { - id = q.getIntField(0, -1); - } - m_findErrorStatement.reset(); - } - - if (id == 0) - { - id = insertElement(); - - m_insertErrorStatement.bind(1, id); - m_insertErrorStatement.bind(2, storageErrorData.message.c_str()); - m_insertErrorStatement.bind(3, storageErrorData.fatal); - m_insertErrorStatement.bind(4, storageErrorData.indexed); - m_insertErrorStatement.bind(5, storageErrorData.translationUnit.c_str()); - executeStatement(m_insertErrorStatement); - id = m_database.lastRowId(); - m_insertErrorStatement.reset(); - } - return id; - } - - void DatabaseStorage::setNodeType(int nodeId, int nodeType) - { - m_setNodeTypeStmt.bind(1, nodeType); - m_setNodeTypeStmt.bind(2, nodeId); - executeStatement(m_setNodeTypeStmt); - m_setNodeTypeStmt.reset(); - } - - void DatabaseStorage::setFileLanguage(int fileId, const std::string& languageIdentifier) - { - m_setFileLanguageStmt.bind(1, languageIdentifier.c_str()); - m_setFileLanguageStmt.bind(2, fileId); - executeStatement(m_setFileLanguageStmt); - m_setFileLanguageStmt.reset(); - } - - // --- Private Interface --- - - void DatabaseStorage::setupTables() - { - executeStatement( - "CREATE TABLE IF NOT EXISTS meta(" - " id INTEGER, " - " key TEXT, " - " value TEXT, " - " PRIMARY KEY(id)" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS element(" - " id INTEGER, " - " PRIMARY KEY(id)" - ");" - ); - - 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, " - " type INTEGER NOT NULL, " - " source_node_id INTEGER NOT NULL, " - " target_node_id INTEGER NOT NULL, " - " PRIMARY KEY(id), " - " FOREIGN KEY(id) REFERENCES element(id) ON DELETE CASCADE, " - " FOREIGN KEY(source_node_id) REFERENCES node(id) ON DELETE CASCADE, " - " FOREIGN KEY(target_node_id) REFERENCES node(id) ON DELETE CASCADE" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS node(" - " id INTEGER NOT NULL, " - " type INTEGER NOT NULL, " - " serialized_name TEXT, " - " PRIMARY KEY(id), " - " FOREIGN KEY(id) REFERENCES element(id) ON DELETE CASCADE" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS symbol(" - " id INTEGER NOT NULL, " - " definition_kind INTEGER NOT NULL, " - " PRIMARY KEY(id), " - " FOREIGN KEY(id) REFERENCES node(id) ON DELETE CASCADE" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS file(" - " id INTEGER NOT NULL, " - " path TEXT, " - " language TEXT, " - " modification_time TEXT, " - " indexed INTEGER, " - " complete INTEGER, " - " line_count INTEGER, " - " PRIMARY KEY(id), " - " FOREIGN KEY(id) REFERENCES node(id) ON DELETE CASCADE" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS filecontent(" - " id INTERGER, " - " content TEXT, " - " FOREIGN KEY(id) REFERENCES file(id)" - " ON DELETE CASCADE " - " ON UPDATE CASCADE" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS local_symbol(" - " id INTEGER NOT NULL, " - " name TEXT, " - " PRIMARY KEY(id), " - " FOREIGN KEY(id) REFERENCES element(id) ON DELETE CASCADE" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS source_location(" - " id INTEGER NOT NULL, " - " file_node_id INTEGER, " - " start_line INTEGER, " - " start_column INTEGER, " - " end_line INTEGER, " - " end_column INTEGER, " - " type INTEGER, " - " PRIMARY KEY(id), " - " FOREIGN KEY(file_node_id) REFERENCES node(id) ON DELETE CASCADE" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS occurrence(" - " element_id INTEGER NOT NULL, " - " source_location_id INTEGER NOT NULL, " - " PRIMARY KEY(element_id, source_location_id), " - " FOREIGN KEY(element_id) REFERENCES element(id) ON DELETE CASCADE, " - " FOREIGN KEY(source_location_id) REFERENCES source_location(id) ON DELETE CASCADE" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS component_access(" - " node_id INTEGER NOT NULL, " - " type INTEGER NOT NULL, " - " PRIMARY KEY(node_id), " - " FOREIGN KEY(node_id) REFERENCES node(id) ON DELETE CASCADE" - ");" - ); - - executeStatement( - "CREATE TABLE IF NOT EXISTS error(" - " id INTEGER NOT NULL, " - " message TEXT, " - " fatal INTEGER NOT NULL, " - " indexed INTEGER NOT NULL, " - " translation_unit TEXT, " - " PRIMARY KEY(id), " - " FOREIGN KEY(id) REFERENCES element(id) ON DELETE CASCADE" - ");" - ); - } - - void DatabaseStorage::clearTables() - { - const std::vector tableNames = { - "meta", - "error", - "component_access", - "occurrence", - "source_location", - "local_symbol", - "filecontent", - "file", - "symbol", - "node", - "edge", - "element_component" - "element" - }; - - for (const std::string& tableName : tableNames) - { - executeStatement("DROP TABLE IF EXISTS main." + tableName + ";"); - } - } - - void DatabaseStorage::setupIndices() - { - executeStatement( - "CREATE INDEX IF NOT EXISTS node_serialized_name_index ON node(serialized_name);" - ); - - executeStatement( - "CREATE INDEX IF NOT EXISTS edge_source_target_type_index ON edge(source_node_id, target_node_id, type);" - ); - - executeStatement( - "CREATE INDEX IF NOT EXISTS local_symbol_name_index ON local_symbol(name);" - ); - - executeStatement( - "CREATE INDEX IF NOT EXISTS source_location_all_data_index " - "ON source_location(file_node_id, start_line, start_column, end_line, end_column, type);" - ); - - executeStatement( - "CREATE INDEX IF NOT EXISTS error_all_data_index ON error(message, fatal);" - ); - } - - void DatabaseStorage::setupPrecompiledStatements() - { - m_insertElementStatement = compileStatement( - "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;" - ); - - m_insertNodeStatement = compileStatement( - "INSERT INTO node(id, type, serialized_name) VALUES(?, ?, ?);" - ); - - m_setNodeTypeStmt = compileStatement( - "UPDATE node SET type = ? WHERE id == ?;" - ); - - m_insertSymbolStatement = compileStatement( - "INSERT OR IGNORE INTO symbol(id, definition_kind) VALUES(?, ?);" - ); - - m_findFileStatement = compileStatement( - "SELECT id FROM file WHERE id == ?;" - ); - - m_insertFileStatement = compileStatement( - "INSERT OR IGNORE INTO file(id, path, language, modification_time, indexed, complete, line_count) VALUES(?, ?, ?, ?, ?, ?, ?);" - ); - - m_setFileLanguageStmt = compileStatement( - "UPDATE file SET language = ? WHERE id == ?;" - ); - - m_insertFileContentStatement = compileStatement( - "INSERT INTO filecontent(id, content) VALUES(?, ?);" - ); - - m_findEdgeStatement = compileStatement( - "SELECT id FROM edge WHERE source_node_id == ? AND target_node_id == ? AND type == ? LIMIT 1;" - ); - - m_insertEdgeStatement = compileStatement( - "INSERT INTO edge(id, type, source_node_id, target_node_id) VALUES(?, ?, ?, ?);" - ); - - m_findLocalSymbolStmt = compileStatement( - "SELECT id FROM local_symbol WHERE name == ? LIMIT 1;" - ); - - m_insertLocalSymbolStmt = compileStatement( - "INSERT INTO local_symbol(id, name) VALUES(?, ?);" - ); - - m_findSourceLocationStmt = compileStatement( - "SELECT id FROM source_location WHERE " - "file_node_id = ? AND " - "start_line = ? AND " - "start_column = ? AND " - "end_line = ? AND " - "end_column = ? AND " - "type = ? " - "LIMIT 1;" - ); - - m_insertSourceLocationStmt = compileStatement( - "INSERT INTO source_location(" - "id, file_node_id, start_line, start_column, end_line, end_column, type) " - "VALUES(NULL, ?, ?, ?, ?, ?, ?);" - ); - - m_insertOccurenceStmt = compileStatement( - "INSERT OR IGNORE INTO occurrence(element_id, source_location_id) VALUES(?, ?);" - ); - - m_findErrorStatement = compileStatement( - "SELECT id FROM error WHERE " - "message = ? AND " - "fatal == ? " - "LIMIT 1;" - ); - - m_insertErrorStatement = compileStatement( - "INSERT INTO error(id, message, fatal, indexed, translation_unit) " - "VALUES(?, ?, ?, ?, ?);" - ); - - m_insertOrUpdateMetaValueStmt = compileStatement( - "INSERT OR REPLACE INTO meta(id, key, value) VALUES(" - "(SELECT id FROM meta WHERE key = ?), ?, ?" - ");" - ); - } - - void DatabaseStorage::clearPrecompiledStatements() - { - m_insertElementStatement.finalize(); - m_insertElementComponentStatement.finalize(); - m_findNodeStatement.finalize(); - m_insertNodeStatement.finalize(); - m_setNodeTypeStmt.finalize(); - m_insertSymbolStatement.finalize(); - m_findFileStatement.finalize(); - m_insertFileStatement.finalize(); - m_setFileLanguageStmt.finalize(); - m_insertFileContentStatement.finalize(); - m_findEdgeStatement.finalize(); - m_insertEdgeStatement.finalize(); - m_findLocalSymbolStmt.finalize(); - m_insertLocalSymbolStmt.finalize(); - m_findSourceLocationStmt.finalize(); - m_insertSourceLocationStmt.finalize(); - m_insertOccurenceStmt.finalize(); - m_findErrorStatement.finalize(); - m_insertErrorStatement.finalize(); - m_insertOrUpdateMetaValueStmt.finalize(); - } - - int DatabaseStorage::insertElement() - { - executeStatement(m_insertElementStatement); - int id = m_database.lastRowId(); - m_insertElementStatement.reset(); - return id; - } - - void DatabaseStorage::insertOrUpdateMetaValue(const std::string& key, const std::string& value) - { - m_insertOrUpdateMetaValueStmt.bind(1, key.c_str()); - m_insertOrUpdateMetaValueStmt.bind(2, key.c_str()); - m_insertOrUpdateMetaValueStmt.bind(3, value.c_str()); - executeStatement(m_insertOrUpdateMetaValueStmt); - m_insertOrUpdateMetaValueStmt.reset(); - } - - CppSQLite3Statement DatabaseStorage::compileStatement(const std::string& statement) const - { - try - { - return m_database.compileStatement(statement.c_str()); - } - catch (CppSQLite3Exception e) - { - throw SourcetrailException("Failed to compile statement \"" + statement + "\" with message \"" + e.errorMessage() + "\"."); - } - - return CppSQLite3Statement(); - } - - void DatabaseStorage::executeStatement(const std::string& statement) const - { - try - { - m_database.execDML(statement.c_str()); - } - catch (CppSQLite3Exception e) - { - throw SourcetrailException("Failed to execute statement \"" + statement + "\" with message \"" + e.errorMessage() + "\"."); - } - } - - void DatabaseStorage::executeStatement(CppSQLite3Statement& statement) const - { - try - { - statement.execDML(); - } - catch (CppSQLite3Exception e) - { - throw SourcetrailException("Failed to execute statement with message \"" + std::string(e.errorMessage()) + "\"."); - } - } - - CppSQLite3Query DatabaseStorage::executeQuery(const std::string& query) const - { - try - { - return m_database.execQuery(query.c_str()); - } - catch (CppSQLite3Exception e) - { - throw SourcetrailException("Failed to execute query \"" + query + "\" with message \"" + e.errorMessage() + "\"."); - } - } - - CppSQLite3Query DatabaseStorage::executeQuery(CppSQLite3Statement& statement) const - { - try - { - return statement.execQuery(); - } - catch (CppSQLite3Exception e) - { - throw SourcetrailException("Failed to execute query with message \"" + std::string(e.errorMessage()) + "\"."); - } - } - - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const - { - CppSQLite3Query q = executeQuery( - "SELECT id, type, source_node_id, target_node_id FROM edge " + query + ";" - ); - - std::vector edges; - while (!q.eof()) - { - const int id = q.getIntField(0, 0); - const int edgeKind = q.getIntField(1, -1); - const int sourceId = q.getIntField(2, 0); - const int targetId = q.getIntField(3, 0); - - if (id != 0 && edgeKind != -1) - { - edges.emplace_back(StorageEdge(id, sourceId, targetId, edgeKind)); - } - - q.nextRow(); - } - return edges; - } - - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const - { - CppSQLite3Query q = executeQuery( - "SELECT id, type, serialized_name FROM node " + query + ";" - ); - - std::vector nodes; - while (!q.eof()) - { - const int id = q.getIntField(0, 0); - const int type = q.getIntField(1, -1); - const std::string serializedName = q.getStringField(2, ""); - - if (id != 0 && type != -1) - { - nodes.emplace_back(StorageNode(id, type, serializedName)); - } - - q.nextRow(); - } - return nodes; - } - - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const - { - CppSQLite3Query q = executeQuery( - "SELECT id, definition_kind FROM symbol " + query + ";" - ); - - std::vector symbols; - while (!q.eof()) - { - const int id = q.getIntField(0, 0); - const int definitionKind = q.getIntField(1, 0); - - if (id != 0) - { - symbols.emplace_back(StorageSymbol(id, definitionKind)); - } - - q.nextRow(); - } - return symbols; - } - - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const - { - CppSQLite3Query q = executeQuery( - "SELECT id, path, language, modification_time, indexed, complete FROM file " + query + ";" - ); - - std::vector files; - while (!q.eof()) - { - const int id = q.getIntField(0, 0); - const std::string filePath = q.getStringField(1, ""); - const std::string languageIdentifier = q.getStringField(2, ""); - const std::string modificationTime = q.getStringField(3, ""); - const bool indexed = q.getIntField(4, 0); - const bool complete = q.getIntField(5, 0); - - if (id != 0) - { - files.emplace_back(StorageFile(id, filePath, languageIdentifier, modificationTime, indexed, complete)); - } - q.nextRow(); - } - - return files; - } - - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const - { - CppSQLite3Query q = executeQuery( - "SELECT id, name FROM local_symbol " + query + ";" - ); - - std::vector localSymbols; - - while (!q.eof()) - { - const int id = q.getIntField(0, 0); - const std::string name = q.getStringField(1, ""); - - if (id != 0) - { - localSymbols.emplace_back(StorageLocalSymbol(id, name)); - } - - q.nextRow(); - } - return localSymbols; - } - - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const - { - CppSQLite3Query q = executeQuery( - "SELECT id, file_node_id, start_line, start_column, end_line, end_column, type FROM source_location " + query + ";" - ); - - std::vector sourceLocations; - - while (!q.eof()) - { - const int id = q.getIntField(0, 0); - const int fileNodeId = q.getIntField(1, 0); - const int startLineNumber = q.getIntField(2, -1); - const int startColNumber = q.getIntField(3, -1); - const int endLineNumber = q.getIntField(4, -1); - const int endColNumber = q.getIntField(5, -1); - const int locationKind = q.getIntField(6, -1); - - if (id != 0 && fileNodeId != 0 && startLineNumber != -1 && startColNumber != -1 && endLineNumber != -1 && - endColNumber != -1 && locationKind != -1) - { - sourceLocations.emplace_back(StorageSourceLocation(id, fileNodeId, startLineNumber, startColNumber, endLineNumber, endColNumber, locationKind)); - } - - q.nextRow(); - } - return sourceLocations; - } - - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const - { - CppSQLite3Query q = executeQuery( - "SELECT element_id, source_location_id FROM occurrence " + query + ";" - ); - - std::vector occurrences; - - while (!q.eof()) - { - const int elementId = q.getIntField(0, 0); - const int sourceLocationId = q.getIntField(1, 0); - - if (elementId != 0 && sourceLocationId != 0) - { - occurrences.emplace_back(StorageOccurrence(elementId, sourceLocationId)); - } - - q.nextRow(); - } - return occurrences; - } - - template <> - std::vector DatabaseStorage::doGetAll(const std::string& query) const - { - CppSQLite3Query q = executeQuery( - "SELECT id, message, fatal, indexed, translation_unit FROM error " + query + ";" - ); - - std::vector errors; - while (!q.eof()) - { - const int id = q.getIntField(0, 0); - const std::string message = q.getStringField(1, ""); - const bool fatal = q.getIntField(2, 0); - const bool indexed = q.getIntField(3, 0); - const std::string translationUnit = q.getStringField(4, ""); - - if (id != 0) - { - errors.emplace_back(StorageError(id, message, translationUnit, fatal, indexed)); - } - - q.nextRow(); - } - - return errors; - } - + return id; } + +void DatabaseStorage::addSymbol(const StorageSymbol& storageSymbol) +{ + m_insertSymbolStatement.bind(1, storageSymbol.id); + m_insertSymbolStatement.bind(2, storageSymbol.definitionKind); + executeStatement(m_insertSymbolStatement); + m_insertSymbolStatement.reset(); +} + +void DatabaseStorage::addFile(const StorageFile& storageFile) +{ + { + m_findFileStatement.bind(1, storageFile.id); + CppSQLite3Query q = executeQuery(m_findFileStatement); + bool exists = !q.eof(); + m_findFileStatement.reset(); + if (exists) + { + return; + } + } + + std::string content = ""; + if (utility::getFileExists(storageFile.filePath)) + { + content = utility::getFileContent(storageFile.filePath); + } + const int lineCount = utility::getLineCount(content); + + { + m_insertFileStatement.bind(1, storageFile.id); + m_insertFileStatement.bind(2, storageFile.filePath.c_str()); + m_insertFileStatement.bind(3, storageFile.languageIdentifier.c_str()); + m_insertFileStatement.bind(4, storageFile.modificationTime.c_str()); + m_insertFileStatement.bind(5, storageFile.indexed); + m_insertFileStatement.bind(6, storageFile.complete); + m_insertFileStatement.bind(7, lineCount); + executeStatement(m_insertFileStatement); + m_insertFileStatement.reset(); + } + + if (!content.empty()) + { + m_insertFileContentStatement.bind(1, storageFile.id); + m_insertFileContentStatement.bind(2, content.c_str()); + executeStatement(m_insertFileContentStatement); + m_insertFileContentStatement.reset(); + } +} + +int DatabaseStorage::addEdge(const StorageEdgeData& storageEdgeData) +{ + int id = 0; + + { + m_findEdgeStatement.bind(1, storageEdgeData.sourceNodeId); + m_findEdgeStatement.bind(2, storageEdgeData.targetNodeId); + m_findEdgeStatement.bind(3, storageEdgeData.edgeKind); + CppSQLite3Query q = executeQuery(m_findEdgeStatement); + if (!q.eof()) + { + id = q.getIntField(0, 0); + } + m_findEdgeStatement.reset(); + } + + if (id == 0) + { + id = insertElement(); + + m_insertEdgeStatement.bind(1, id); + m_insertEdgeStatement.bind(2, storageEdgeData.edgeKind); + m_insertEdgeStatement.bind(3, storageEdgeData.sourceNodeId); + m_insertEdgeStatement.bind(4, storageEdgeData.targetNodeId); + executeStatement(m_insertEdgeStatement); + m_insertEdgeStatement.reset(); + } + return id; +} + +int DatabaseStorage::addLocalSymbol(const StorageLocalSymbolData& storageLocalSymbolData) +{ + int id = 0; + + { + m_findLocalSymbolStmt.bind(1, storageLocalSymbolData.name.c_str()); + CppSQLite3Query q = executeQuery(m_findLocalSymbolStmt); + if (!q.eof()) + { + id = q.getIntField(0, 0); + } + m_findLocalSymbolStmt.reset(); + } + + if (id == 0) + { + id = insertElement(); + + m_insertLocalSymbolStmt.bind(1, id); + m_insertLocalSymbolStmt.bind(2, storageLocalSymbolData.name.c_str()); + executeStatement(m_insertLocalSymbolStmt); + m_insertLocalSymbolStmt.reset(); + } + return id; +} + +int DatabaseStorage::addSourceLocation(const StorageSourceLocationData& storageSourceLocationData) +{ + int id = 0; + + { + m_findSourceLocationStmt.bind(1, storageSourceLocationData.fileNodeId); + m_findSourceLocationStmt.bind(2, storageSourceLocationData.startLineNumber); + m_findSourceLocationStmt.bind(3, storageSourceLocationData.startColumnNumber); + m_findSourceLocationStmt.bind(4, storageSourceLocationData.endLineNumber); + m_findSourceLocationStmt.bind(5, storageSourceLocationData.endColumnNumber); + m_findSourceLocationStmt.bind(6, storageSourceLocationData.locationKind); + CppSQLite3Query q = executeQuery(m_findSourceLocationStmt); + if (!q.eof()) + { + id = q.getIntField(0, 0); + } + m_findSourceLocationStmt.reset(); + } + + if (id == 0) + { + m_insertSourceLocationStmt.bind(1, storageSourceLocationData.fileNodeId); + m_insertSourceLocationStmt.bind(2, storageSourceLocationData.startLineNumber); + m_insertSourceLocationStmt.bind(3, storageSourceLocationData.startColumnNumber); + m_insertSourceLocationStmt.bind(4, storageSourceLocationData.endLineNumber); + m_insertSourceLocationStmt.bind(5, storageSourceLocationData.endColumnNumber); + m_insertSourceLocationStmt.bind(6, storageSourceLocationData.locationKind); + executeStatement(m_insertSourceLocationStmt); + id = m_database.lastRowId(); + m_insertSourceLocationStmt.reset(); + } + return id; +} + +void DatabaseStorage::addOccurrence(const StorageOccurrence& storageOccurrence) +{ + m_insertOccurenceStmt.bind(1, storageOccurrence.elementId); + m_insertOccurenceStmt.bind(2, storageOccurrence.sourceLocationId); + executeStatement(m_insertOccurenceStmt); + m_insertOccurenceStmt.reset(); +} + +int DatabaseStorage::addError(const StorageErrorData& storageErrorData) +{ + int id = 0; + { + m_findErrorStatement.bind(1, storageErrorData.message.c_str()); + m_findErrorStatement.bind(2, storageErrorData.fatal); + CppSQLite3Query q = executeQuery(m_findErrorStatement); + if (!q.eof() && q.numFields() > 0) + { + id = q.getIntField(0, -1); + } + m_findErrorStatement.reset(); + } + + if (id == 0) + { + id = insertElement(); + + m_insertErrorStatement.bind(1, id); + m_insertErrorStatement.bind(2, storageErrorData.message.c_str()); + m_insertErrorStatement.bind(3, storageErrorData.fatal); + m_insertErrorStatement.bind(4, storageErrorData.indexed); + m_insertErrorStatement.bind(5, storageErrorData.translationUnit.c_str()); + executeStatement(m_insertErrorStatement); + id = m_database.lastRowId(); + m_insertErrorStatement.reset(); + } + return id; +} + +void DatabaseStorage::setNodeType(int nodeId, int nodeType) +{ + m_setNodeTypeStmt.bind(1, nodeType); + m_setNodeTypeStmt.bind(2, nodeId); + executeStatement(m_setNodeTypeStmt); + m_setNodeTypeStmt.reset(); +} + +void DatabaseStorage::setFileLanguage(int fileId, const std::string& languageIdentifier) +{ + m_setFileLanguageStmt.bind(1, languageIdentifier.c_str()); + m_setFileLanguageStmt.bind(2, fileId); + executeStatement(m_setFileLanguageStmt); + m_setFileLanguageStmt.reset(); +} + +// --- Private Interface --- + +void DatabaseStorage::setupTables() +{ + executeStatement( + "CREATE TABLE IF NOT EXISTS meta(" + " id INTEGER, " + " key TEXT, " + " value TEXT, " + " PRIMARY KEY(id)" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS element(" + " id INTEGER, " + " PRIMARY KEY(id)" + ");"); + + 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, " + " type INTEGER NOT NULL, " + " source_node_id INTEGER NOT NULL, " + " target_node_id INTEGER NOT NULL, " + " PRIMARY KEY(id), " + " FOREIGN KEY(id) REFERENCES element(id) ON DELETE CASCADE, " + " FOREIGN KEY(source_node_id) REFERENCES node(id) ON DELETE CASCADE, " + " FOREIGN KEY(target_node_id) REFERENCES node(id) ON DELETE CASCADE" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS node(" + " id INTEGER NOT NULL, " + " type INTEGER NOT NULL, " + " serialized_name TEXT, " + " PRIMARY KEY(id), " + " FOREIGN KEY(id) REFERENCES element(id) ON DELETE CASCADE" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS symbol(" + " id INTEGER NOT NULL, " + " definition_kind INTEGER NOT NULL, " + " PRIMARY KEY(id), " + " FOREIGN KEY(id) REFERENCES node(id) ON DELETE CASCADE" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS file(" + " id INTEGER NOT NULL, " + " path TEXT, " + " language TEXT, " + " modification_time TEXT, " + " indexed INTEGER, " + " complete INTEGER, " + " line_count INTEGER, " + " PRIMARY KEY(id), " + " FOREIGN KEY(id) REFERENCES node(id) ON DELETE CASCADE" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS filecontent(" + " id INTERGER, " + " content TEXT, " + " FOREIGN KEY(id) REFERENCES file(id)" + " ON DELETE CASCADE " + " ON UPDATE CASCADE" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS local_symbol(" + " id INTEGER NOT NULL, " + " name TEXT, " + " PRIMARY KEY(id), " + " FOREIGN KEY(id) REFERENCES element(id) ON DELETE CASCADE" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS source_location(" + " id INTEGER NOT NULL, " + " file_node_id INTEGER, " + " start_line INTEGER, " + " start_column INTEGER, " + " end_line INTEGER, " + " end_column INTEGER, " + " type INTEGER, " + " PRIMARY KEY(id), " + " FOREIGN KEY(file_node_id) REFERENCES node(id) ON DELETE CASCADE" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS occurrence(" + " element_id INTEGER NOT NULL, " + " source_location_id INTEGER NOT NULL, " + " PRIMARY KEY(element_id, source_location_id), " + " FOREIGN KEY(element_id) REFERENCES element(id) ON DELETE CASCADE, " + " FOREIGN KEY(source_location_id) REFERENCES source_location(id) ON DELETE CASCADE" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS component_access(" + " node_id INTEGER NOT NULL, " + " type INTEGER NOT NULL, " + " PRIMARY KEY(node_id), " + " FOREIGN KEY(node_id) REFERENCES node(id) ON DELETE CASCADE" + ");"); + + executeStatement( + "CREATE TABLE IF NOT EXISTS error(" + " id INTEGER NOT NULL, " + " message TEXT, " + " fatal INTEGER NOT NULL, " + " indexed INTEGER NOT NULL, " + " translation_unit TEXT, " + " PRIMARY KEY(id), " + " FOREIGN KEY(id) REFERENCES element(id) ON DELETE CASCADE" + ");"); +} + +void DatabaseStorage::clearTables() +{ + const std::vector tableNames = { + "meta", + "error", + "component_access", + "occurrence", + "source_location", + "local_symbol", + "filecontent", + "file", + "symbol", + "node", + "edge", + "element_component" + "element"}; + + for (const std::string& tableName: tableNames) + { + executeStatement("DROP TABLE IF EXISTS main." + tableName + ";"); + } +} + +void DatabaseStorage::setupIndices() +{ + executeStatement("CREATE INDEX IF NOT EXISTS node_serialized_name_index ON node(serialized_name);"); + + executeStatement("CREATE INDEX IF NOT EXISTS edge_source_target_type_index ON edge(source_node_id, target_node_id, type);"); + + executeStatement("CREATE INDEX IF NOT EXISTS local_symbol_name_index ON local_symbol(name);"); + + executeStatement( + "CREATE INDEX IF NOT EXISTS source_location_all_data_index " + "ON source_location(file_node_id, start_line, start_column, end_line, end_column, type);"); + + executeStatement("CREATE INDEX IF NOT EXISTS error_all_data_index ON error(message, fatal);"); +} + +void DatabaseStorage::setupPrecompiledStatements() +{ + m_insertElementStatement = compileStatement("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;"); + + m_insertNodeStatement = compileStatement("INSERT INTO node(id, type, serialized_name) VALUES(?, ?, ?);"); + + m_setNodeTypeStmt = compileStatement("UPDATE node SET type = ? WHERE id == ?;"); + + m_insertSymbolStatement = compileStatement("INSERT OR IGNORE INTO symbol(id, definition_kind) VALUES(?, ?);"); + + m_findFileStatement = compileStatement("SELECT id FROM file WHERE id == ?;"); + + m_insertFileStatement = compileStatement( + "INSERT OR IGNORE INTO file(id, path, language, modification_time, indexed, complete, line_count) VALUES(?, ?, ?, ?, ?, ?, ?);"); + + m_setFileLanguageStmt = compileStatement("UPDATE file SET language = ? WHERE id == ?;"); + + m_insertFileContentStatement = compileStatement("INSERT INTO filecontent(id, content) VALUES(?, ?);"); + + m_findEdgeStatement = compileStatement("SELECT id FROM edge WHERE source_node_id == ? AND target_node_id == ? AND type == ? LIMIT 1;"); + + m_insertEdgeStatement = compileStatement("INSERT INTO edge(id, type, source_node_id, target_node_id) VALUES(?, ?, ?, ?);"); + + m_findLocalSymbolStmt = compileStatement("SELECT id FROM local_symbol WHERE name == ? LIMIT 1;"); + + m_insertLocalSymbolStmt = compileStatement("INSERT INTO local_symbol(id, name) VALUES(?, ?);"); + + m_findSourceLocationStmt = compileStatement( + "SELECT id FROM source_location WHERE " + "file_node_id = ? AND " + "start_line = ? AND " + "start_column = ? AND " + "end_line = ? AND " + "end_column = ? AND " + "type = ? " + "LIMIT 1;"); + + m_insertSourceLocationStmt = compileStatement( + "INSERT INTO source_location(" + "id, file_node_id, start_line, start_column, end_line, end_column, type) " + "VALUES(NULL, ?, ?, ?, ?, ?, ?);"); + + m_insertOccurenceStmt = compileStatement("INSERT OR IGNORE INTO occurrence(element_id, source_location_id) VALUES(?, ?);"); + + m_findErrorStatement = compileStatement( + "SELECT id FROM error WHERE " + "message = ? AND " + "fatal == ? " + "LIMIT 1;"); + + m_insertErrorStatement = compileStatement( + "INSERT INTO error(id, message, fatal, indexed, translation_unit) " + "VALUES(?, ?, ?, ?, ?);"); + + m_insertOrUpdateMetaValueStmt = compileStatement( + "INSERT OR REPLACE INTO meta(id, key, value) VALUES(" + "(SELECT id FROM meta WHERE key = ?), ?, ?" + ");"); +} + +void DatabaseStorage::clearPrecompiledStatements() +{ + m_insertElementStatement.finalize(); + m_insertElementComponentStatement.finalize(); + m_findNodeStatement.finalize(); + m_insertNodeStatement.finalize(); + m_setNodeTypeStmt.finalize(); + m_insertSymbolStatement.finalize(); + m_findFileStatement.finalize(); + m_insertFileStatement.finalize(); + m_setFileLanguageStmt.finalize(); + m_insertFileContentStatement.finalize(); + m_findEdgeStatement.finalize(); + m_insertEdgeStatement.finalize(); + m_findLocalSymbolStmt.finalize(); + m_insertLocalSymbolStmt.finalize(); + m_findSourceLocationStmt.finalize(); + m_insertSourceLocationStmt.finalize(); + m_insertOccurenceStmt.finalize(); + m_findErrorStatement.finalize(); + m_insertErrorStatement.finalize(); + m_insertOrUpdateMetaValueStmt.finalize(); +} + +int DatabaseStorage::insertElement() +{ + executeStatement(m_insertElementStatement); + int id = m_database.lastRowId(); + m_insertElementStatement.reset(); + return id; +} + +void DatabaseStorage::insertOrUpdateMetaValue(const std::string& key, const std::string& value) +{ + m_insertOrUpdateMetaValueStmt.bind(1, key.c_str()); + m_insertOrUpdateMetaValueStmt.bind(2, key.c_str()); + m_insertOrUpdateMetaValueStmt.bind(3, value.c_str()); + executeStatement(m_insertOrUpdateMetaValueStmt); + m_insertOrUpdateMetaValueStmt.reset(); +} + +CppSQLite3Statement DatabaseStorage::compileStatement(const std::string& statement) const +{ + try + { + return m_database.compileStatement(statement.c_str()); + } + catch (CppSQLite3Exception e) + { + throw SourcetrailException("Failed to compile statement \"" + statement + "\" with message \"" + e.errorMessage() + "\"."); + } + + return CppSQLite3Statement(); +} + +void DatabaseStorage::executeStatement(const std::string& statement) const +{ + try + { + m_database.execDML(statement.c_str()); + } + catch (CppSQLite3Exception e) + { + throw SourcetrailException("Failed to execute statement \"" + statement + "\" with message \"" + e.errorMessage() + "\"."); + } +} + +void DatabaseStorage::executeStatement(CppSQLite3Statement& statement) const +{ + try + { + statement.execDML(); + } + catch (CppSQLite3Exception e) + { + throw SourcetrailException("Failed to execute statement with message \"" + std::string(e.errorMessage()) + "\"."); + } +} + +CppSQLite3Query DatabaseStorage::executeQuery(const std::string& query) const +{ + try + { + return m_database.execQuery(query.c_str()); + } + catch (CppSQLite3Exception e) + { + throw SourcetrailException("Failed to execute query \"" + query + "\" with message \"" + e.errorMessage() + "\"."); + } +} + +CppSQLite3Query DatabaseStorage::executeQuery(CppSQLite3Statement& statement) const +{ + try + { + return statement.execQuery(); + } + catch (CppSQLite3Exception e) + { + throw SourcetrailException("Failed to execute query with message \"" + std::string(e.errorMessage()) + "\"."); + } +} + +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const +{ + CppSQLite3Query q = executeQuery("SELECT id, type, source_node_id, target_node_id FROM edge " + query + ";"); + + std::vector edges; + while (!q.eof()) + { + const int id = q.getIntField(0, 0); + const int edgeKind = q.getIntField(1, -1); + const int sourceId = q.getIntField(2, 0); + const int targetId = q.getIntField(3, 0); + + if (id != 0 && edgeKind != -1) + { + edges.emplace_back(StorageEdge(id, sourceId, targetId, edgeKind)); + } + + q.nextRow(); + } + return edges; +} + +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const +{ + CppSQLite3Query q = executeQuery("SELECT id, type, serialized_name FROM node " + query + ";"); + + std::vector nodes; + while (!q.eof()) + { + const int id = q.getIntField(0, 0); + const int type = q.getIntField(1, -1); + const std::string serializedName = q.getStringField(2, ""); + + if (id != 0 && type != -1) + { + nodes.emplace_back(StorageNode(id, type, serializedName)); + } + + q.nextRow(); + } + return nodes; +} + +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const +{ + CppSQLite3Query q = executeQuery("SELECT id, definition_kind FROM symbol " + query + ";"); + + std::vector symbols; + while (!q.eof()) + { + const int id = q.getIntField(0, 0); + const int definitionKind = q.getIntField(1, 0); + + if (id != 0) + { + symbols.emplace_back(StorageSymbol(id, definitionKind)); + } + + q.nextRow(); + } + return symbols; +} + +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const +{ + CppSQLite3Query q = executeQuery("SELECT id, path, language, modification_time, indexed, complete FROM file " + query + ";"); + + std::vector files; + while (!q.eof()) + { + const int id = q.getIntField(0, 0); + const std::string filePath = q.getStringField(1, ""); + const std::string languageIdentifier = q.getStringField(2, ""); + const std::string modificationTime = q.getStringField(3, ""); + const bool indexed = q.getIntField(4, 0); + const bool complete = q.getIntField(5, 0); + + if (id != 0) + { + files.emplace_back(StorageFile(id, filePath, languageIdentifier, modificationTime, indexed, complete)); + } + q.nextRow(); + } + + return files; +} + +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const +{ + CppSQLite3Query q = executeQuery("SELECT id, name FROM local_symbol " + query + ";"); + + std::vector localSymbols; + + while (!q.eof()) + { + const int id = q.getIntField(0, 0); + const std::string name = q.getStringField(1, ""); + + if (id != 0) + { + localSymbols.emplace_back(StorageLocalSymbol(id, name)); + } + + q.nextRow(); + } + return localSymbols; +} + +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const +{ + CppSQLite3Query q = executeQuery( + "SELECT id, file_node_id, start_line, start_column, end_line, end_column, type FROM source_location " + query + ";"); + + std::vector sourceLocations; + + while (!q.eof()) + { + const int id = q.getIntField(0, 0); + const int fileNodeId = q.getIntField(1, 0); + const int startLineNumber = q.getIntField(2, -1); + const int startColNumber = q.getIntField(3, -1); + const int endLineNumber = q.getIntField(4, -1); + const int endColNumber = q.getIntField(5, -1); + const int locationKind = q.getIntField(6, -1); + + if (id != 0 && fileNodeId != 0 && startLineNumber != -1 && startColNumber != -1 && endLineNumber != -1 && endColNumber != -1 && + locationKind != -1) + { + sourceLocations.emplace_back( + StorageSourceLocation(id, fileNodeId, startLineNumber, startColNumber, endLineNumber, endColNumber, locationKind)); + } + + q.nextRow(); + } + return sourceLocations; +} + +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const +{ + CppSQLite3Query q = executeQuery("SELECT element_id, source_location_id FROM occurrence " + query + ";"); + + std::vector occurrences; + + while (!q.eof()) + { + const int elementId = q.getIntField(0, 0); + const int sourceLocationId = q.getIntField(1, 0); + + if (elementId != 0 && sourceLocationId != 0) + { + occurrences.emplace_back(StorageOccurrence(elementId, sourceLocationId)); + } + + q.nextRow(); + } + return occurrences; +} + +template <> +std::vector DatabaseStorage::doGetAll(const std::string& query) const +{ + CppSQLite3Query q = executeQuery("SELECT id, message, fatal, indexed, translation_unit FROM error " + query + ";"); + + std::vector errors; + while (!q.eof()) + { + const int id = q.getIntField(0, 0); + const std::string message = q.getStringField(1, ""); + const bool fatal = q.getIntField(2, 0); + const bool indexed = q.getIntField(3, 0); + const std::string translationUnit = q.getStringField(4, ""); + + if (id != 0) + { + errors.emplace_back(StorageError(id, message, translationUnit, fatal, indexed)); + } + + q.nextRow(); + } + + return errors; +} + +} // namespace sourcetrail diff --git a/core/src/DefinitionKind.cpp b/core/src/DefinitionKind.cpp index 42c6a55..f62a4cc 100644 --- a/core/src/DefinitionKind.cpp +++ b/core/src/DefinitionKind.cpp @@ -1,40 +1,40 @@ /* -* 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. -*/ + * 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 "DefinitionKind.h" namespace sourcetrail { - int definitionKindToInt(DefinitionKind kind) - { - return static_cast(kind); - } - - DefinitionKind intToDefinitionKind(int i) - { - const DefinitionKind kinds[] = { DefinitionKind::IMPLICIT, DefinitionKind::EXPLICIT }; - - for (DefinitionKind kind : kinds) - { - if (i == definitionKindToInt(kind)) - { - return kind; - } - } - - return DefinitionKind::EXPLICIT; - } +int definitionKindToInt(DefinitionKind kind) +{ + return static_cast(kind); } + +DefinitionKind intToDefinitionKind(int i) +{ + const DefinitionKind kinds[] = {DefinitionKind::IMPLICIT, DefinitionKind::EXPLICIT}; + + for (DefinitionKind kind: kinds) + { + if (i == definitionKindToInt(kind)) + { + return kind; + } + } + + return DefinitionKind::EXPLICIT; +} +} // namespace sourcetrail diff --git a/core/src/EdgeKind.cpp b/core/src/EdgeKind.cpp index ab859c1..fab639c 100644 --- a/core/src/EdgeKind.cpp +++ b/core/src/EdgeKind.cpp @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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 "EdgeKind.h" @@ -20,35 +20,34 @@ namespace sourcetrail { - int edgeKindToInt(EdgeKind kind) - { - return static_cast(kind); - } - - EdgeKind intToEdgeKind(int i) - { - const EdgeKind kinds[] = { - EdgeKind::MEMBER, - EdgeKind::TYPE_USAGE, - EdgeKind::USAGE, - EdgeKind::CALL, - EdgeKind::INHERITANCE, - EdgeKind::OVERRIDE, - EdgeKind::TYPE_ARGUMENT, - EdgeKind::TEMPLATE_SPECIALIZATION, - EdgeKind::INCLUDE, - EdgeKind::IMPORT, - EdgeKind::MACRO_USAGE - }; - - for (EdgeKind kind : kinds) - { - if (i == edgeKindToInt(kind)) - { - return kind; - } - } - - return EdgeKind::UNKNOWN; - } +int edgeKindToInt(EdgeKind kind) +{ + return static_cast(kind); } + +EdgeKind intToEdgeKind(int i) +{ + const EdgeKind kinds[] = { + EdgeKind::MEMBER, + EdgeKind::TYPE_USAGE, + EdgeKind::USAGE, + EdgeKind::CALL, + EdgeKind::INHERITANCE, + EdgeKind::OVERRIDE, + EdgeKind::TYPE_ARGUMENT, + EdgeKind::TEMPLATE_SPECIALIZATION, + EdgeKind::INCLUDE, + EdgeKind::IMPORT, + EdgeKind::MACRO_USAGE}; + + for (EdgeKind kind: kinds) + { + if (i == edgeKindToInt(kind)) + { + return kind; + } + } + + return EdgeKind::UNKNOWN; +} +} // namespace sourcetrail diff --git a/core/src/ElementComponentKind.cpp b/core/src/ElementComponentKind.cpp index b82dbc5..e75a63a 100644 --- a/core/src/ElementComponentKind.cpp +++ b/core/src/ElementComponentKind.cpp @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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" @@ -20,25 +20,23 @@ namespace sourcetrail { - int elementComponentKindToInt(ElementComponentKind kind) - { - return static_cast(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."); - } +int elementComponentKindToInt(ElementComponentKind kind) +{ + return static_cast(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."); +} +} // namespace sourcetrail diff --git a/core/src/LocationKind.cpp b/core/src/LocationKind.cpp index 957081e..d04aa41 100644 --- a/core/src/LocationKind.cpp +++ b/core/src/LocationKind.cpp @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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 "LocationKind.h" @@ -20,34 +20,33 @@ namespace sourcetrail { - int locationKindToInt(LocationKind kind) - { - return static_cast(kind); - } - - LocationKind intToLocationKind(int i) - { - const LocationKind kinds[] = { - LocationKind::TOKEN, - LocationKind::SCOPE, - LocationKind::QUALIFIER, - LocationKind::LOCAL_SYMBOL, - LocationKind::SIGNATURE, - LocationKind::ATOMIC_RANGE, - LocationKind::INDEXER_ERROR, - LocationKind::FULLTEXT_SEARCH, - LocationKind::SCREEN_SEARCH, - LocationKind::UNSOLVED - }; - - for (LocationKind kind : kinds) - { - if (i == locationKindToInt(kind)) - { - return kind; - } - } - - throw SourcetrailException("Unable to convert integer \"" + std::to_string(i) + "\" to location kind."); - } +int locationKindToInt(LocationKind kind) +{ + return static_cast(kind); } + +LocationKind intToLocationKind(int i) +{ + const LocationKind kinds[] = { + LocationKind::TOKEN, + LocationKind::SCOPE, + LocationKind::QUALIFIER, + LocationKind::LOCAL_SYMBOL, + LocationKind::SIGNATURE, + LocationKind::ATOMIC_RANGE, + LocationKind::INDEXER_ERROR, + LocationKind::FULLTEXT_SEARCH, + LocationKind::SCREEN_SEARCH, + LocationKind::UNSOLVED}; + + for (LocationKind kind: kinds) + { + if (i == locationKindToInt(kind)) + { + return kind; + } + } + + throw SourcetrailException("Unable to convert integer \"" + std::to_string(i) + "\" to location kind."); +} +} // namespace sourcetrail diff --git a/core/src/NameHierarchy.cpp b/core/src/NameHierarchy.cpp index 4034c00..7ca2b33 100644 --- a/core/src/NameHierarchy.cpp +++ b/core/src/NameHierarchy.cpp @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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 "NameHierarchy.h" @@ -20,111 +20,106 @@ namespace sourcetrail { - std::string serializeNameHierarchyToJson(const NameHierarchy& nameHierarchy) +std::string serializeNameHierarchyToJson(const NameHierarchy& nameHierarchy) +{ + typedef nlohmann::json json; + + nlohmann::json j; + + j["name_delimiter"] = nameHierarchy.nameDelimiter; + + for (const NameElement& nameElement: nameHierarchy.nameElements) { - typedef nlohmann::json json; - - nlohmann::json j; - - j["name_delimiter"] = nameHierarchy.nameDelimiter; - - for (const NameElement& nameElement : nameHierarchy.nameElements) - { - j["name_elements"].push_back( - { - { "prefix", nameElement.prefix } , - { "name", nameElement.name } , - { "postfix", nameElement.postfix } - }); - } - - return j.dump(4); + j["name_elements"].push_back({{"prefix", nameElement.prefix}, {"name", nameElement.name}, {"postfix", nameElement.postfix}}); } - NameHierarchy deserializeNameHierarchyFromJson(const std::string& serializedNameHierarchy, std::string* error) - { - typedef nlohmann::json json; - - NameHierarchy nameHierarchy; - try - { - json j = nlohmann::json::parse(serializedNameHierarchy); - { - json jDelimiter = j["name_delimiter"]; - if (jDelimiter.is_string()) - { - nameHierarchy.nameDelimiter = jDelimiter.get(); - } - } - - { - json jNameElements = j["name_elements"]; - if (jNameElements.is_array()) - { - for (json::iterator it = jNameElements.begin(); it != jNameElements.end(); ++it) - { - NameElement nameElement; - { - json jPrefix = it.value()["prefix"]; - if (jPrefix.is_string()) - { - nameElement.prefix = jPrefix.get(); - } - } - { - json jName = it.value()["name"]; - if (jName.is_string()) - { - nameElement.name = jName.get(); - } - } - { - json jPostfix = it.value()["postfix"]; - if (jPostfix.is_string()) - { - nameElement.postfix = jPostfix.get(); - } - } - nameHierarchy.nameElements.push_back(nameElement); - } - } - } - } - catch (json::exception& e) - { - if (error) - { - *error = e.what(); - } - } - catch (...) - { - if (error) - { - *error = "Exception thrown during NameHierarchy deserialization"; - } - } - - return nameHierarchy; - } - - std::string serializeNameHierarchyToDatabaseString(const NameHierarchy& nameHierarchy) - { - static std::string META_DELIMITER = "\tm"; - static std::string NAME_DELIMITER = "\tn"; - static std::string PARTS_DELIMITER = "\ts"; - static std::string SIGNATURE_DELIMITER = "\tp"; - - std::string serialized = nameHierarchy.nameDelimiter + META_DELIMITER; - for (size_t i = 0; i < nameHierarchy.nameElements.size(); i++) - { - if (i != 0) - { - serialized += NAME_DELIMITER; - } - const NameElement& nameElement = nameHierarchy.nameElements[i]; - serialized += nameElement.name + PARTS_DELIMITER + nameElement.prefix + SIGNATURE_DELIMITER + nameElement.postfix; - } - return serialized; - } + return j.dump(4); } + +NameHierarchy deserializeNameHierarchyFromJson(const std::string& serializedNameHierarchy, std::string* error) +{ + typedef nlohmann::json json; + + NameHierarchy nameHierarchy; + try + { + json j = nlohmann::json::parse(serializedNameHierarchy); + { + json jDelimiter = j["name_delimiter"]; + if (jDelimiter.is_string()) + { + nameHierarchy.nameDelimiter = jDelimiter.get(); + } + } + + { + json jNameElements = j["name_elements"]; + if (jNameElements.is_array()) + { + for (json::iterator it = jNameElements.begin(); it != jNameElements.end(); ++it) + { + NameElement nameElement; + { + json jPrefix = it.value()["prefix"]; + if (jPrefix.is_string()) + { + nameElement.prefix = jPrefix.get(); + } + } + { + json jName = it.value()["name"]; + if (jName.is_string()) + { + nameElement.name = jName.get(); + } + } + { + json jPostfix = it.value()["postfix"]; + if (jPostfix.is_string()) + { + nameElement.postfix = jPostfix.get(); + } + } + nameHierarchy.nameElements.push_back(nameElement); + } + } + } + } + catch (json::exception& e) + { + if (error) + { + *error = e.what(); + } + } + catch (...) + { + if (error) + { + *error = "Exception thrown during NameHierarchy deserialization"; + } + } + + return nameHierarchy; +} + +std::string serializeNameHierarchyToDatabaseString(const NameHierarchy& nameHierarchy) +{ + static std::string META_DELIMITER = "\tm"; + static std::string NAME_DELIMITER = "\tn"; + static std::string PARTS_DELIMITER = "\ts"; + static std::string SIGNATURE_DELIMITER = "\tp"; + + std::string serialized = nameHierarchy.nameDelimiter + META_DELIMITER; + for (size_t i = 0; i < nameHierarchy.nameElements.size(); i++) + { + if (i != 0) + { + serialized += NAME_DELIMITER; + } + const NameElement& nameElement = nameHierarchy.nameElements[i]; + serialized += nameElement.name + PARTS_DELIMITER + nameElement.prefix + SIGNATURE_DELIMITER + nameElement.postfix; + } + return serialized; +} +} // namespace sourcetrail diff --git a/core/src/NodeKind.cpp b/core/src/NodeKind.cpp index 137c396..e35290c 100644 --- a/core/src/NodeKind.cpp +++ b/core/src/NodeKind.cpp @@ -1,62 +1,60 @@ /* -* 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. -*/ + * 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 "NodeKind.h" namespace sourcetrail { - int nodeKindToInt(NodeKind kind) - { - return static_cast(kind); - } - - NodeKind intToNodeKind(int i) - { - const NodeKind kinds[] = { - NodeKind::UNKNOWN, - NodeKind::TYPE, - NodeKind::BUILTIN_TYPE, - NodeKind::MODULE, - NodeKind::NAMESPACE, - NodeKind::PACKAGE, - NodeKind::STRUCT, - NodeKind::CLASS, - NodeKind::INTERFACE, - NodeKind::ANNOTATION, - NodeKind::GLOBAL_VARIABLE, - NodeKind::FIELD, - NodeKind::FUNCTION, - NodeKind::METHOD, - NodeKind::ENUM, - NodeKind::ENUM_CONSTANT, - NodeKind::TYPEDEF, - NodeKind::TYPE_PARAMETER, - NodeKind::FILE, - NodeKind::MACRO, - NodeKind::UNION - }; - - for (NodeKind kind : kinds) - { - if (i == nodeKindToInt(kind)) - { - return kind; - } - } - - return NodeKind::UNKNOWN; - } +int nodeKindToInt(NodeKind kind) +{ + return static_cast(kind); } + +NodeKind intToNodeKind(int i) +{ + const NodeKind kinds[] = {NodeKind::UNKNOWN, + NodeKind::TYPE, + NodeKind::BUILTIN_TYPE, + NodeKind::MODULE, + NodeKind::NAMESPACE, + NodeKind::PACKAGE, + NodeKind::STRUCT, + NodeKind::CLASS, + NodeKind::INTERFACE, + NodeKind::ANNOTATION, + NodeKind::GLOBAL_VARIABLE, + NodeKind::FIELD, + NodeKind::FUNCTION, + NodeKind::METHOD, + NodeKind::ENUM, + NodeKind::ENUM_CONSTANT, + NodeKind::TYPEDEF, + NodeKind::TYPE_PARAMETER, + NodeKind::FILE, + NodeKind::MACRO, + NodeKind::UNION}; + + for (NodeKind kind: kinds) + { + if (i == nodeKindToInt(kind)) + { + return kind; + } + } + + return NodeKind::UNKNOWN; +} +} // namespace sourcetrail diff --git a/core/src/ReferenceKind.cpp b/core/src/ReferenceKind.cpp index c6d8fac..b40babb 100644 --- a/core/src/ReferenceKind.cpp +++ b/core/src/ReferenceKind.cpp @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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 "ReferenceKind.h" @@ -20,33 +20,33 @@ namespace sourcetrail { - EdgeKind referenceKindToEdgeKind(ReferenceKind kind) +EdgeKind referenceKindToEdgeKind(ReferenceKind kind) +{ + switch (kind) { - switch (kind) - { - case ReferenceKind::TYPE_USAGE: - return EdgeKind::TYPE_USAGE; - case ReferenceKind::USAGE: - return EdgeKind::USAGE; - case ReferenceKind::CALL: - return EdgeKind::CALL; - case ReferenceKind::INHERITANCE: - return EdgeKind::INHERITANCE; - case ReferenceKind::OVERRIDE: - return EdgeKind::OVERRIDE; - case ReferenceKind::TYPE_ARGUMENT: - return EdgeKind::TYPE_ARGUMENT; - case ReferenceKind::TEMPLATE_SPECIALIZATION: - return EdgeKind::TEMPLATE_SPECIALIZATION; - case ReferenceKind::INCLUDE: - return EdgeKind::INCLUDE; - case ReferenceKind::IMPORT: - return EdgeKind::IMPORT; - case ReferenceKind::MACRO_USAGE: - return EdgeKind::MACRO_USAGE; - case ReferenceKind::ANNOTATION_USAGE: - return EdgeKind::ANNOTATION_USAGE; - } - return EdgeKind::UNKNOWN; + case ReferenceKind::TYPE_USAGE: + return EdgeKind::TYPE_USAGE; + case ReferenceKind::USAGE: + return EdgeKind::USAGE; + case ReferenceKind::CALL: + return EdgeKind::CALL; + case ReferenceKind::INHERITANCE: + return EdgeKind::INHERITANCE; + case ReferenceKind::OVERRIDE: + return EdgeKind::OVERRIDE; + case ReferenceKind::TYPE_ARGUMENT: + return EdgeKind::TYPE_ARGUMENT; + case ReferenceKind::TEMPLATE_SPECIALIZATION: + return EdgeKind::TEMPLATE_SPECIALIZATION; + case ReferenceKind::INCLUDE: + return EdgeKind::INCLUDE; + case ReferenceKind::IMPORT: + return EdgeKind::IMPORT; + case ReferenceKind::MACRO_USAGE: + return EdgeKind::MACRO_USAGE; + case ReferenceKind::ANNOTATION_USAGE: + return EdgeKind::ANNOTATION_USAGE; } + return EdgeKind::UNKNOWN; } +} // namespace sourcetrail diff --git a/core/src/SourcetrailDBWriter.cpp b/core/src/SourcetrailDBWriter.cpp index 98dfb04..ab5ab10 100644 --- a/core/src/SourcetrailDBWriter.cpp +++ b/core/src/SourcetrailDBWriter.cpp @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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 "SourcetrailDBWriter.h" @@ -37,807 +37,785 @@ namespace sourcetrail { - // --- Public Interface --- +// --- Public Interface --- - SourcetrailDBWriter::SourcetrailDBWriter() - : m_lastError("") +SourcetrailDBWriter::SourcetrailDBWriter(): m_lastError("") {} + +SourcetrailDBWriter::~SourcetrailDBWriter() {} + +std::string SourcetrailDBWriter::getVersionString() const +{ + return VERSION_STRING; +} + +int SourcetrailDBWriter::getSupportedDatabaseVersion() const +{ + return DatabaseStorage::getSupportedDatabaseVersion(); +} + +const std::string& SourcetrailDBWriter::getLastError() const +{ + return m_lastError; +} + +void SourcetrailDBWriter::setLastError(const std::string& error) const +{ + m_lastError = error; +} + +void SourcetrailDBWriter::clearLastError() +{ + m_lastError.clear(); +} + +bool SourcetrailDBWriter::open(const std::string& databaseFilePath) +{ + m_databaseFilePath = databaseFilePath; + + m_projectFilePath = databaseFilePath; // FIXME: only find dot after last slash/backslash! otherwise it may be part of the file path + const size_t pos = m_projectFilePath.rfind('.'); + if (pos != std::string::npos) { + m_projectFilePath = m_projectFilePath.substr(0, pos); } + m_projectFilePath += ".srctrlprj"; - SourcetrailDBWriter::~SourcetrailDBWriter() { - } - - std::string SourcetrailDBWriter::getVersionString() const - { - return VERSION_STRING; - } - - int SourcetrailDBWriter::getSupportedDatabaseVersion() const - { - return DatabaseStorage::getSupportedDatabaseVersion(); - } - - const std::string& SourcetrailDBWriter::getLastError() const - { - return m_lastError; - } - - void SourcetrailDBWriter::setLastError(const std::string& error) const - { - m_lastError = error; - } - - void SourcetrailDBWriter::clearLastError() - { - m_lastError.clear(); - } - - bool SourcetrailDBWriter::open(const std::string& databaseFilePath) - { - m_databaseFilePath = databaseFilePath; - - m_projectFilePath = databaseFilePath; //FIXME: only find dot after last slash/backslash! otherwise it may be part of the file path - const size_t pos = m_projectFilePath.rfind('.'); - if (pos != std::string::npos) - { - m_projectFilePath = m_projectFilePath.substr(0, pos); - } - m_projectFilePath += ".srctrlprj"; - - { - bool projectFileExists = false; - { - std::ifstream f(m_projectFilePath.c_str()); - projectFileExists = f.good(); - f.close(); - } - if (!projectFileExists) - { - try - { - createOrResetProjectFile(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - } - - try - { - openDatabase(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - try - { - setupDatabaseTables(); - updateProjectSettingsText(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - bool SourcetrailDBWriter::close() - { - try - { - closeDatabase(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - bool SourcetrailDBWriter::clear() - { - try - { - clearDatabaseTables(); - updateProjectSettingsText(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - bool SourcetrailDBWriter::isEmpty() const - { - if (!m_storage) - { - m_lastError = "Unable to check if database is empty, because no database is currently open."; - return true; - } - - try - { - return m_storage->isEmpty(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return true; - } - } - - bool SourcetrailDBWriter::isCompatible() const - { - if (!m_storage) - { - m_lastError = "Unable to check if database is compatible, because no database is currently open."; - return false; - } - - try - { - return m_storage->isCompatible(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - int SourcetrailDBWriter::getLoadedDatabaseVersion() const - { - if (!m_storage) - { - m_lastError = "Unable to fetch database version, because no database is currently open."; - return false; - } - - try - { - return m_storage->getLoadedDatabaseVersion(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return 0; - } - } - - bool SourcetrailDBWriter::beginTransaction() - { - if (!m_storage) - { - m_lastError = "Unable to begin transaction, because no database is currently open."; - return false; - } - - try - { - m_storage->beginTransaction(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - bool SourcetrailDBWriter::commitTransaction() - { - if (!m_storage) - { - m_lastError = "Unable to commit transaction, because no database is currently open."; - return false; - } - - try - { - m_storage->commitTransaction(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - bool SourcetrailDBWriter::rollbackTransaction() - { - if (!m_storage) - { - m_lastError = "Unable to rollback transaction, because no database is currently open."; - return false; - } - - try - { - m_storage->rollbackTransaction(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - bool SourcetrailDBWriter::optimizeDatabaseMemory() - { - if (!m_storage) - { - m_lastError = "Unable to optimize database memory, because no database is currently open."; - return false; - } - - try - { - m_storage->optimizeDatabaseMemory(); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - int SourcetrailDBWriter::recordSymbol(const NameHierarchy& nameHierarchy) - { - if (!m_storage) - { - m_lastError = "Unable to record symbol, because no database is currently open."; - return false; - } - - try - { - return addNodeHierarchy(nameHierarchy); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return 0; - } - } - - bool SourcetrailDBWriter::recordSymbolDefinitionKind(int symbolId, DefinitionKind definitionKind) - { - if (!m_storage) - { - m_lastError = "Unable to record symbol kind, because no database is currently open."; - return false; - } - - try - { - m_storage->addSymbol(StorageSymbol(symbolId, definitionKindToInt(definitionKind))); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - bool SourcetrailDBWriter::recordSymbolKind(int symbolId, SymbolKind symbolKind) - { - if (!m_storage) - { - m_lastError = "Unable to record symbol kind, because no database is currently open."; - return false; - } - - try - { - m_storage->setNodeType(symbolId, nodeKindToInt(symbolKindToNodeKind(symbolKind))); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - bool SourcetrailDBWriter::recordSymbolLocation(int symbolId, const SourceRange& location) - { - if (!m_storage) - { - m_lastError = "Unable to record symbol location, because no database is currently open."; - return false; - } - - try - { - addSourceLocation(symbolId, location, LocationKind::TOKEN); - return true; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - bool SourcetrailDBWriter::recordSymbolScopeLocation(int symbolId, const SourceRange& location) - { - if (!m_storage) - { - m_lastError = "Unable to record symbol scope location, because no database is currently open."; - return false; - } - - try - { - addSourceLocation(symbolId, location, LocationKind::SCOPE); - return true; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - bool SourcetrailDBWriter::recordSymbolSignatureLocation(int symbolId, const SourceRange& location) - { - if (!m_storage) - { - m_lastError = "Unable to record symbol signature location, because no database is currently open."; - return false; - } - - try - { - addSourceLocation(symbolId, location, LocationKind::SIGNATURE); - return true; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - int SourcetrailDBWriter::recordReference(int contextSymbolId, int referencedSymbolId, ReferenceKind referenceKind) - { - if (!m_storage) - { - m_lastError = "Unable to record reference, because no database is currently open."; - return 0; - } - - try - { - return addEdge(contextSymbolId, referencedSymbolId, referenceKindToEdgeKind(referenceKind)); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return 0; - } - } - - bool SourcetrailDBWriter::recordReferenceLocation(int referenceId, const SourceRange& location) - { - if (!m_storage) - { - m_lastError = "Unable to record symbol reference location, because no database is currently open."; - return false; - } - - try - { - addSourceLocation(referenceId, location, LocationKind::TOKEN); - return true; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - bool SourcetrailDBWriter::recordReferenceIsAmbiguous(int referenceId) - { - if (!m_storage) - { - m_lastError = "Unable to record ambiguity of reference, because no database is currently open."; - return false; - } - - try - { - addElementComponent(referenceId, ElementComponentKind::IS_AMBIGUOUS, ""); - return false; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - int SourcetrailDBWriter::recordReferenceToUnsolvedSymhol(int contextSymbolId, ReferenceKind referenceKind, const SourceRange& location) - { - if (!m_storage) - { - m_lastError = "Unable to record symbol reference, because no database is currently open."; - return 0; - } - - try - { - NameHierarchy unsolvedSymbolName; - NameElement unsolvedSymbolNameElement; - unsolvedSymbolNameElement.name = "unsolved symbol"; - unsolvedSymbolName.nameElements.push_back(unsolvedSymbolNameElement); - int unsolvedSymbolId = addNodeHierarchy(unsolvedSymbolName); - int referenceId = addEdge(contextSymbolId, unsolvedSymbolId, referenceKindToEdgeKind(referenceKind)); - addSourceLocation(referenceId, location, LocationKind::UNSOLVED); - return referenceId; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return 0; - } - } - - bool SourcetrailDBWriter::recordQualifierLocation(int referencedSymbolId, const SourceRange& location) - { - if (!m_storage) - { - m_lastError = "Unable to record symbol qualifier location, because no database is currently open."; - return false; - } - - try - { - addSourceLocation(referencedSymbolId, location, LocationKind::QUALIFIER); - return true; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - int SourcetrailDBWriter::recordFile(const std::string& filePath) - { - if (!m_storage) - { - m_lastError = "Unable to record file, because no database is currently open."; - return false; - } - - try - { - return addFile(filePath); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return 0; - } - } - - bool SourcetrailDBWriter::recordFileLanguage(int fileId, const std::string& languageIdentifier) - { - if (!m_storage) - { - m_lastError = "Unable to record file language, because no database is currently open."; - return false; - } - - try - { - m_storage->setFileLanguage(fileId, languageIdentifier); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - - return true; - } - - int SourcetrailDBWriter::recordLocalSymbol(const std::string& name) - { - if (!m_storage) - { - m_lastError = "Unable to record local symbol, because no database is currently open."; - return false; - } - - try - { - return m_storage->addLocalSymbol(StorageLocalSymbolData(name)); - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return 0; - } - } - - bool SourcetrailDBWriter::recordLocalSymbolLocation(int localSymbolId, const SourceRange& location) - { - if (!m_storage) - { - m_lastError = "Unable to record local symbol location, because no database is currently open."; - return false; - } - - try - { - addSourceLocation(localSymbolId, location, LocationKind::LOCAL_SYMBOL); - return true; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - bool SourcetrailDBWriter::recordAtomicSourceRange(const SourceRange& sourceRange) - { - if (!m_storage) - { - m_lastError = "Unable to record atomic source range, because no database is currently open."; - return false; - } - - try - { - const int sourceLocationId = m_storage->addSourceLocation(StorageSourceLocationData( - sourceRange.fileId, - sourceRange.startLine, - sourceRange.startColumn, - sourceRange.endLine, - sourceRange.endColumn, - locationKindToInt(LocationKind::ATOMIC_RANGE) - )); - - return true; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - bool SourcetrailDBWriter::recordError(const std::string& message, bool fatal, const SourceRange& location) - { - if (!m_storage) - { - m_lastError = "Unable to record error, because no database is currently open."; - return false; - } - - try - { - const int errorId = m_storage->addError(StorageErrorData(message, "", fatal, true)); - addSourceLocation(errorId, location, LocationKind::INDEXER_ERROR); - return true; - } - catch (const SourcetrailException e) - { - m_lastError = e.getMessage(); - return false; - } - } - - // --- Private Interface --- - - void SourcetrailDBWriter::openDatabase() - { - if (m_storage) - { - closeDatabase(); - } - - try - { - m_storage = DatabaseStorage::openDatabase(m_databaseFilePath); - } - catch (CppSQLite3Exception e) - { - m_storage.reset(); - throw e; - } - } - - void SourcetrailDBWriter::closeDatabase() - { - if (!m_storage) - { - throw SourcetrailException("Unable to close database, because no database is currently open."); - } - m_storage.reset(); - } - - void SourcetrailDBWriter::setupDatabaseTables() - { - if (!m_storage) - { - throw SourcetrailException("Unable to setup database tables, because no database is currently open."); - } - m_storage->setupDatabase(); - } - - void SourcetrailDBWriter::clearDatabaseTables() - { - if (!m_storage) - { - throw SourcetrailException("Unable to setup database tables, because no database is currently open."); - } - m_storage->clearDatabase(); - } - - void SourcetrailDBWriter::createOrResetProjectFile() - { - try - { - std::ofstream fileStream; - fileStream.open(m_projectFilePath, std::ios::out); - fileStream << std::string( - "\n" - "\n" - " 0\n" - "\n" - ); - fileStream.close(); - } - catch (...) - { - throw SourcetrailException("Exception occurred while creating project file."); - } - } - - void SourcetrailDBWriter::updateProjectSettingsText() - { - try + bool projectFileExists = false; { std::ifstream f(m_projectFilePath.c_str()); - if (f.is_open()) + projectFileExists = f.good(); + f.close(); + } + if (!projectFileExists) + { + try { - std::string line; - std::string text; - while (getline(f, line)) - { - text += line + '\n'; - } - f.close(); - m_storage->setProjectSettingsText(text); + createOrResetProjectFile(); } - else + catch (const SourcetrailException e) { - throw SourcetrailException("Unable to open project file."); + m_lastError = e.getMessage(); + return false; } } - catch (...) - { - throw SourcetrailException("Exception occurred while creating project file."); - } } - int SourcetrailDBWriter::addNodeHierarchy(const NameHierarchy& nameHierarchy) + try { - if (nameHierarchy.nameElements.size() == 0) - { - throw SourcetrailException("Unable to add nodes for an empty name hierarchy."); - } - - int parentNodeId = 0; - - NameHierarchy currentNameHierarchy; - currentNameHierarchy.nameDelimiter = nameHierarchy.nameDelimiter; - - for (size_t i = 0; i < nameHierarchy.nameElements.size(); i++) - { - currentNameHierarchy.nameElements.push_back(nameHierarchy.nameElements[i]); - - int nodeId = m_storage->addNode(StorageNodeData( - nodeKindToInt(NodeKind::UNKNOWN), - serializeNameHierarchyToDatabaseString(currentNameHierarchy) - )); - - if (parentNodeId != 0) - { - addEdge(parentNodeId, nodeId, EdgeKind::MEMBER); - } - - parentNodeId = nodeId; - } - return parentNodeId; + openDatabase(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; } - int SourcetrailDBWriter::addFile(const std::string& filePath) + try { - NameElement nameElement; - nameElement.name = filePath; - NameHierarchy nameHierarchy; - nameHierarchy.nameDelimiter = "/"; - nameHierarchy.nameElements.push_back(nameElement); - - const int nodeId = addNodeHierarchy(nameHierarchy); - m_storage->setNodeType(nodeId, nodeKindToInt(NodeKind::FILE)); - - m_storage->addFile(StorageFile(nodeId, filePath, "", utility::getDateTimeString(time(0)), true, true)); - - return nodeId; + setupDatabaseTables(); + updateProjectSettingsText(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; } - int SourcetrailDBWriter::addEdge(int sourceId, int targetId, EdgeKind edgeKind) - { - if (!m_storage) - { - throw SourcetrailException("Unable to add edge, because no database is currently open."); - } - if (!sourceId) - { - throw SourcetrailException("Unable to add edge, because source id is invalid."); - } - if (!targetId) - { - throw SourcetrailException("Unable to add edge, because target id is invalid."); - } + return true; +} - return m_storage->addEdge(StorageEdgeData(sourceId, targetId, edgeKindToInt(edgeKind))); +bool SourcetrailDBWriter::close() +{ + try + { + closeDatabase(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; } - void SourcetrailDBWriter::addSourceLocation(int elementId, const SourceRange& location, LocationKind kind) - { - const int sourceLocationId = m_storage->addSourceLocation(StorageSourceLocationData( - location.fileId, - location.startLine, - location.startColumn, - location.endLine, - location.endColumn, - locationKindToInt(kind) - )); + return true; +} - m_storage->addOccurrence(StorageOccurrence( - elementId, - sourceLocationId - )); +bool SourcetrailDBWriter::clear() +{ + try + { + clearDatabaseTables(); + updateProjectSettingsText(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; } - void SourcetrailDBWriter::addElementComponent(int elementId, ElementComponentKind kind, const std::string& data) + return true; +} + +bool SourcetrailDBWriter::isEmpty() const +{ + if (!m_storage) { - const int sourceLocationId = m_storage->addElementComponent(StorageElementComponentData( - elementId, - elementComponentKindToInt(kind), - data - )); + m_lastError = "Unable to check if database is empty, because no database is currently open."; + return true; + } + + try + { + return m_storage->isEmpty(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return true; } } + +bool SourcetrailDBWriter::isCompatible() const +{ + if (!m_storage) + { + m_lastError = "Unable to check if database is compatible, because no database is currently open."; + return false; + } + + try + { + return m_storage->isCompatible(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +int SourcetrailDBWriter::getLoadedDatabaseVersion() const +{ + if (!m_storage) + { + m_lastError = "Unable to fetch database version, because no database is currently open."; + return false; + } + + try + { + return m_storage->getLoadedDatabaseVersion(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return 0; + } +} + +bool SourcetrailDBWriter::beginTransaction() +{ + if (!m_storage) + { + m_lastError = "Unable to begin transaction, because no database is currently open."; + return false; + } + + try + { + m_storage->beginTransaction(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } + + return true; +} + +bool SourcetrailDBWriter::commitTransaction() +{ + if (!m_storage) + { + m_lastError = "Unable to commit transaction, because no database is currently open."; + return false; + } + + try + { + m_storage->commitTransaction(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } + + return true; +} + +bool SourcetrailDBWriter::rollbackTransaction() +{ + if (!m_storage) + { + m_lastError = "Unable to rollback transaction, because no database is currently open."; + return false; + } + + try + { + m_storage->rollbackTransaction(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } + + return true; +} + +bool SourcetrailDBWriter::optimizeDatabaseMemory() +{ + if (!m_storage) + { + m_lastError = "Unable to optimize database memory, because no database is currently open."; + return false; + } + + try + { + m_storage->optimizeDatabaseMemory(); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } + + return true; +} + +int SourcetrailDBWriter::recordSymbol(const NameHierarchy& nameHierarchy) +{ + if (!m_storage) + { + m_lastError = "Unable to record symbol, because no database is currently open."; + return false; + } + + try + { + return addNodeHierarchy(nameHierarchy); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return 0; + } +} + +bool SourcetrailDBWriter::recordSymbolDefinitionKind(int symbolId, DefinitionKind definitionKind) +{ + if (!m_storage) + { + m_lastError = "Unable to record symbol kind, because no database is currently open."; + return false; + } + + try + { + m_storage->addSymbol(StorageSymbol(symbolId, definitionKindToInt(definitionKind))); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } + + return true; +} + +bool SourcetrailDBWriter::recordSymbolKind(int symbolId, SymbolKind symbolKind) +{ + if (!m_storage) + { + m_lastError = "Unable to record symbol kind, because no database is currently open."; + return false; + } + + try + { + m_storage->setNodeType(symbolId, nodeKindToInt(symbolKindToNodeKind(symbolKind))); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } + + return true; +} + +bool SourcetrailDBWriter::recordSymbolLocation(int symbolId, const SourceRange& location) +{ + if (!m_storage) + { + m_lastError = "Unable to record symbol location, because no database is currently open."; + return false; + } + + try + { + addSourceLocation(symbolId, location, LocationKind::TOKEN); + return true; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +bool SourcetrailDBWriter::recordSymbolScopeLocation(int symbolId, const SourceRange& location) +{ + if (!m_storage) + { + m_lastError = "Unable to record symbol scope location, because no database is currently open."; + return false; + } + + try + { + addSourceLocation(symbolId, location, LocationKind::SCOPE); + return true; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +bool SourcetrailDBWriter::recordSymbolSignatureLocation(int symbolId, const SourceRange& location) +{ + if (!m_storage) + { + m_lastError = "Unable to record symbol signature location, because no database is currently open."; + return false; + } + + try + { + addSourceLocation(symbolId, location, LocationKind::SIGNATURE); + return true; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +int SourcetrailDBWriter::recordReference(int contextSymbolId, int referencedSymbolId, ReferenceKind referenceKind) +{ + if (!m_storage) + { + m_lastError = "Unable to record reference, because no database is currently open."; + return 0; + } + + try + { + return addEdge(contextSymbolId, referencedSymbolId, referenceKindToEdgeKind(referenceKind)); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return 0; + } +} + +bool SourcetrailDBWriter::recordReferenceLocation(int referenceId, const SourceRange& location) +{ + if (!m_storage) + { + m_lastError = "Unable to record symbol reference location, because no database is currently open."; + return false; + } + + try + { + addSourceLocation(referenceId, location, LocationKind::TOKEN); + return true; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +bool SourcetrailDBWriter::recordReferenceIsAmbiguous(int referenceId) +{ + if (!m_storage) + { + m_lastError = "Unable to record ambiguity of reference, because no database is currently open."; + return false; + } + + try + { + addElementComponent(referenceId, ElementComponentKind::IS_AMBIGUOUS, ""); + return false; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +int SourcetrailDBWriter::recordReferenceToUnsolvedSymhol(int contextSymbolId, ReferenceKind referenceKind, const SourceRange& location) +{ + if (!m_storage) + { + m_lastError = "Unable to record symbol reference, because no database is currently open."; + return 0; + } + + try + { + NameHierarchy unsolvedSymbolName; + NameElement unsolvedSymbolNameElement; + unsolvedSymbolNameElement.name = "unsolved symbol"; + unsolvedSymbolName.nameElements.push_back(unsolvedSymbolNameElement); + int unsolvedSymbolId = addNodeHierarchy(unsolvedSymbolName); + int referenceId = addEdge(contextSymbolId, unsolvedSymbolId, referenceKindToEdgeKind(referenceKind)); + addSourceLocation(referenceId, location, LocationKind::UNSOLVED); + return referenceId; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return 0; + } +} + +bool SourcetrailDBWriter::recordQualifierLocation(int referencedSymbolId, const SourceRange& location) +{ + if (!m_storage) + { + m_lastError = "Unable to record symbol qualifier location, because no database is currently open."; + return false; + } + + try + { + addSourceLocation(referencedSymbolId, location, LocationKind::QUALIFIER); + return true; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +int SourcetrailDBWriter::recordFile(const std::string& filePath) +{ + if (!m_storage) + { + m_lastError = "Unable to record file, because no database is currently open."; + return false; + } + + try + { + return addFile(filePath); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return 0; + } +} + +bool SourcetrailDBWriter::recordFileLanguage(int fileId, const std::string& languageIdentifier) +{ + if (!m_storage) + { + m_lastError = "Unable to record file language, because no database is currently open."; + return false; + } + + try + { + m_storage->setFileLanguage(fileId, languageIdentifier); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } + + return true; +} + +int SourcetrailDBWriter::recordLocalSymbol(const std::string& name) +{ + if (!m_storage) + { + m_lastError = "Unable to record local symbol, because no database is currently open."; + return false; + } + + try + { + return m_storage->addLocalSymbol(StorageLocalSymbolData(name)); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return 0; + } +} + +bool SourcetrailDBWriter::recordLocalSymbolLocation(int localSymbolId, const SourceRange& location) +{ + if (!m_storage) + { + m_lastError = "Unable to record local symbol location, because no database is currently open."; + return false; + } + + try + { + addSourceLocation(localSymbolId, location, LocationKind::LOCAL_SYMBOL); + return true; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +bool SourcetrailDBWriter::recordAtomicSourceRange(const SourceRange& sourceRange) +{ + if (!m_storage) + { + m_lastError = "Unable to record atomic source range, because no database is currently open."; + return false; + } + + try + { + const int sourceLocationId = m_storage->addSourceLocation(StorageSourceLocationData( + sourceRange.fileId, + sourceRange.startLine, + sourceRange.startColumn, + sourceRange.endLine, + sourceRange.endColumn, + locationKindToInt(LocationKind::ATOMIC_RANGE))); + + return true; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +bool SourcetrailDBWriter::recordError(const std::string& message, bool fatal, const SourceRange& location) +{ + if (!m_storage) + { + m_lastError = "Unable to record error, because no database is currently open."; + return false; + } + + try + { + const int errorId = m_storage->addError(StorageErrorData(message, "", fatal, true)); + addSourceLocation(errorId, location, LocationKind::INDEXER_ERROR); + return true; + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } +} + +// --- Private Interface --- + +void SourcetrailDBWriter::openDatabase() +{ + if (m_storage) + { + closeDatabase(); + } + + try + { + m_storage = DatabaseStorage::openDatabase(m_databaseFilePath); + } + catch (CppSQLite3Exception e) + { + m_storage.reset(); + throw e; + } +} + +void SourcetrailDBWriter::closeDatabase() +{ + if (!m_storage) + { + throw SourcetrailException("Unable to close database, because no database is currently open."); + } + m_storage.reset(); +} + +void SourcetrailDBWriter::setupDatabaseTables() +{ + if (!m_storage) + { + throw SourcetrailException("Unable to setup database tables, because no database is currently open."); + } + m_storage->setupDatabase(); +} + +void SourcetrailDBWriter::clearDatabaseTables() +{ + if (!m_storage) + { + throw SourcetrailException("Unable to setup database tables, because no database is currently open."); + } + m_storage->clearDatabase(); +} + +void SourcetrailDBWriter::createOrResetProjectFile() +{ + try + { + std::ofstream fileStream; + fileStream.open(m_projectFilePath, std::ios::out); + fileStream << std::string( + "\n" + "\n" + " 0\n" + "\n"); + fileStream.close(); + } + catch (...) + { + throw SourcetrailException("Exception occurred while creating project file."); + } +} + +void SourcetrailDBWriter::updateProjectSettingsText() +{ + try + { + std::ifstream f(m_projectFilePath.c_str()); + if (f.is_open()) + { + std::string line; + std::string text; + while (getline(f, line)) + { + text += line + '\n'; + } + f.close(); + m_storage->setProjectSettingsText(text); + } + else + { + throw SourcetrailException("Unable to open project file."); + } + } + catch (...) + { + throw SourcetrailException("Exception occurred while creating project file."); + } +} + +int SourcetrailDBWriter::addNodeHierarchy(const NameHierarchy& nameHierarchy) +{ + if (nameHierarchy.nameElements.size() == 0) + { + throw SourcetrailException("Unable to add nodes for an empty name hierarchy."); + } + + int parentNodeId = 0; + + NameHierarchy currentNameHierarchy; + currentNameHierarchy.nameDelimiter = nameHierarchy.nameDelimiter; + + for (size_t i = 0; i < nameHierarchy.nameElements.size(); i++) + { + currentNameHierarchy.nameElements.push_back(nameHierarchy.nameElements[i]); + + int nodeId = m_storage->addNode( + StorageNodeData(nodeKindToInt(NodeKind::UNKNOWN), serializeNameHierarchyToDatabaseString(currentNameHierarchy))); + + if (parentNodeId != 0) + { + addEdge(parentNodeId, nodeId, EdgeKind::MEMBER); + } + + parentNodeId = nodeId; + } + return parentNodeId; +} + +int SourcetrailDBWriter::addFile(const std::string& filePath) +{ + NameElement nameElement; + nameElement.name = filePath; + NameHierarchy nameHierarchy; + nameHierarchy.nameDelimiter = "/"; + nameHierarchy.nameElements.push_back(nameElement); + + const int nodeId = addNodeHierarchy(nameHierarchy); + m_storage->setNodeType(nodeId, nodeKindToInt(NodeKind::FILE)); + + m_storage->addFile(StorageFile(nodeId, filePath, "", utility::getDateTimeString(time(0)), true, true)); + + return nodeId; +} + +int SourcetrailDBWriter::addEdge(int sourceId, int targetId, EdgeKind edgeKind) +{ + if (!m_storage) + { + throw SourcetrailException("Unable to add edge, because no database is currently open."); + } + if (!sourceId) + { + throw SourcetrailException("Unable to add edge, because source id is invalid."); + } + if (!targetId) + { + throw SourcetrailException("Unable to add edge, because target id is invalid."); + } + + return m_storage->addEdge(StorageEdgeData(sourceId, targetId, edgeKindToInt(edgeKind))); +} + +void SourcetrailDBWriter::addSourceLocation(int elementId, const SourceRange& location, LocationKind kind) +{ + const int sourceLocationId = m_storage->addSourceLocation(StorageSourceLocationData( + location.fileId, location.startLine, location.startColumn, location.endLine, location.endColumn, locationKindToInt(kind))); + + m_storage->addOccurrence(StorageOccurrence(elementId, sourceLocationId)); +} + +void SourcetrailDBWriter::addElementComponent(int elementId, ElementComponentKind kind, const std::string& data) +{ + const int sourceLocationId = m_storage->addElementComponent(StorageElementComponentData(elementId, elementComponentKindToInt(kind), data)); +} +} // namespace sourcetrail diff --git a/core/src/SymbolKind.cpp b/core/src/SymbolKind.cpp index 508b7f0..1adcb7a 100644 --- a/core/src/SymbolKind.cpp +++ b/core/src/SymbolKind.cpp @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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 "SymbolKind.h" @@ -20,49 +20,49 @@ namespace sourcetrail { - NodeKind symbolKindToNodeKind(SymbolKind kind) +NodeKind symbolKindToNodeKind(SymbolKind kind) +{ + switch (kind) { - switch (kind) - { - case SymbolKind::TYPE: - return NodeKind::TYPE; - case SymbolKind::BUILTIN_TYPE: - return NodeKind::BUILTIN_TYPE; - case SymbolKind::MODULE: - return NodeKind::MODULE; - case SymbolKind::NAMESPACE: - return NodeKind::NAMESPACE; - case SymbolKind::PACKAGE: - return NodeKind::PACKAGE; - case SymbolKind::STRUCT: - return NodeKind::STRUCT; - case SymbolKind::CLASS: - return NodeKind::CLASS; - case SymbolKind::INTERFACE: - return NodeKind::INTERFACE; - case SymbolKind::ANNOTATION: - return NodeKind::ANNOTATION; - case SymbolKind::GLOBAL_VARIABLE: - return NodeKind::GLOBAL_VARIABLE; - case SymbolKind::FIELD: - return NodeKind::FIELD; - case SymbolKind::FUNCTION: - return NodeKind::FUNCTION; - case SymbolKind::METHOD: - return NodeKind::METHOD; - case SymbolKind::ENUM: - return NodeKind::ENUM; - case SymbolKind::ENUM_CONSTANT: - return NodeKind::ENUM_CONSTANT; - case SymbolKind::TYPEDEF: - return NodeKind::TYPEDEF; - case SymbolKind::TYPE_PARAMETER: - return NodeKind::TYPE_PARAMETER; - case SymbolKind::MACRO: - return NodeKind::MACRO; - case SymbolKind::UNION: - return NodeKind::UNION; - } - return NodeKind::UNKNOWN; + case SymbolKind::TYPE: + return NodeKind::TYPE; + case SymbolKind::BUILTIN_TYPE: + return NodeKind::BUILTIN_TYPE; + case SymbolKind::MODULE: + return NodeKind::MODULE; + case SymbolKind::NAMESPACE: + return NodeKind::NAMESPACE; + case SymbolKind::PACKAGE: + return NodeKind::PACKAGE; + case SymbolKind::STRUCT: + return NodeKind::STRUCT; + case SymbolKind::CLASS: + return NodeKind::CLASS; + case SymbolKind::INTERFACE: + return NodeKind::INTERFACE; + case SymbolKind::ANNOTATION: + return NodeKind::ANNOTATION; + case SymbolKind::GLOBAL_VARIABLE: + return NodeKind::GLOBAL_VARIABLE; + case SymbolKind::FIELD: + return NodeKind::FIELD; + case SymbolKind::FUNCTION: + return NodeKind::FUNCTION; + case SymbolKind::METHOD: + return NodeKind::METHOD; + case SymbolKind::ENUM: + return NodeKind::ENUM; + case SymbolKind::ENUM_CONSTANT: + return NodeKind::ENUM_CONSTANT; + case SymbolKind::TYPEDEF: + return NodeKind::TYPEDEF; + case SymbolKind::TYPE_PARAMETER: + return NodeKind::TYPE_PARAMETER; + case SymbolKind::MACRO: + return NodeKind::MACRO; + case SymbolKind::UNION: + return NodeKind::UNION; } + return NodeKind::UNKNOWN; } +} // namespace sourcetrail diff --git a/core/src/utility.cpp b/core/src/utility.cpp index 0b9d829..4d9f810 100644 --- a/core/src/utility.cpp +++ b/core/src/utility.cpp @@ -1,18 +1,18 @@ /* -* 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. -*/ + * 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 "utility.h" @@ -24,47 +24,47 @@ namespace sourcetrail { - namespace utility - { - bool getFileExists(const std::string& filePath) - { - std::ifstream file(filePath); - return file.good(); - } - - std::string getFileContent(const std::string& filePath) - { - std::string content; - - std::ifstream file; - file.open(filePath); - - if (file.fail()) - { - throw SourcetrailException("Could not open file " + filePath); - } - - for (std::string line; std::getline(file, line); ) - { - content += line + "\n"; - } - - file.close(); - - return content; - } - - std::string getDateTimeString(const time_t& time) - { - std::tm* ptm = std::localtime(&time); - char buffer[32]; - std::strftime(buffer, 32, "%Y-%m-%d %H:%M:%S", ptm); - return buffer; - } - - int getLineCount(const std::string s) - { - return std::count(s.begin(), s.end(), '\n'); - } - } +namespace utility +{ +bool getFileExists(const std::string& filePath) +{ + std::ifstream file(filePath); + return file.good(); } + +std::string getFileContent(const std::string& filePath) +{ + std::string content; + + std::ifstream file; + file.open(filePath); + + if (file.fail()) + { + throw SourcetrailException("Could not open file " + filePath); + } + + for (std::string line; std::getline(file, line);) + { + content += line + "\n"; + } + + file.close(); + + return content; +} + +std::string getDateTimeString(const time_t& time) +{ + std::tm* ptm = std::localtime(&time); + char buffer[32]; + std::strftime(buffer, 32, "%Y-%m-%d %H:%M:%S", ptm); + return buffer; +} + +int getLineCount(const std::string s) +{ + return std::count(s.begin(), s.end(), '\n'); +} +} // namespace utility +} // namespace sourcetrail