src: Refactored TokenLocation classes to SourceLocation classes

* Removed TokenLocationLine
* decoupled TokenLocationFile from TokenLocationCollection
* save vector of Token ids to SourceLocation
* refactored SourceLocationFile::getFilteredByLines()
* refactored snippet creation
This commit is contained in:
Eberhard Graether
2017-03-25 02:05:48 +01:00
parent 4c64822504
commit 4e9ba897ac
53 changed files with 1453 additions and 1796 deletions
+1 -1
View File
@@ -23,11 +23,11 @@ add_files(
SettingsTestSuite.h
SettingsMigratorTestSuite.h
SearchIndexTestSuite.h
SourceLocationCollectionTestSuite.h
SqliteStorageTestSuite.h
StorageTestSuite.h
TaskSchedulerTestSuite.h
TextAccessTestSuite.h
TokenLocationCollectionTestSuite.h
UtilityStringTestSuite.h
Vector2TestSuite.h
JavaParserTestSuite.h
@@ -0,0 +1,170 @@
#include "cxxtest/TestSuite.h"
#include "data/location/SourceLocation.h"
#include "data/location/SourceLocationCollection.h"
#include "data/location/SourceLocationFile.h"
class SourceLocationCollectionTestSuite : public CxxTest::TestSuite
{
public:
void test_source_locations_get_created_with_other_end()
{
SourceLocationCollection collection;
const SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 4, 5);
TS_ASSERT(a);
TS_ASSERT(a->isStartLocation());
TS_ASSERT(!a->isEndLocation());
const SourceLocation* b = a->getOtherLocation();
TS_ASSERT(b);
TS_ASSERT(!b->isStartLocation());
TS_ASSERT(b->isEndLocation());
TS_ASSERT_EQUALS(a, b->getOtherLocation());
TS_ASSERT_EQUALS(a, b->getStartLocation());
TS_ASSERT_EQUALS(a, a->getStartLocation());
TS_ASSERT_EQUALS(b, a->getEndLocation());
TS_ASSERT_EQUALS(b, b->getEndLocation());
}
void test_source_locations_do_not_get_created_with_wrong_input()
{
SourceLocationCollection collection;
SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 2, 1);
SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {1}, "file.c", 4, 1, 1, 10);
TS_ASSERT(!a);
TS_ASSERT(!b);
}
void test_source_locations_get_unique_id_but_both_ends_have_the_same()
{
SourceLocationCollection collection;
SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 1, 1, 1, 1);
SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {2}, "file.c", 1, 1, 1, 1);
SourceLocation* c = collection.addSourceLocation(LOCATION_TOKEN, 3, {3}, "file.c", 1, 1, 1, 1);
TS_ASSERT_EQUALS(1, collection.getSourceLocationFileCount());
TS_ASSERT_EQUALS(3, collection.getSourceLocationCount());
TS_ASSERT_EQUALS(a->getLocationId(), 1);
TS_ASSERT_EQUALS(b->getLocationId(), 2);
TS_ASSERT_EQUALS(c->getLocationId(), 3);
TS_ASSERT_DIFFERS(a->getLocationId(), b->getLocationId());
TS_ASSERT_DIFFERS(b->getLocationId(), c->getLocationId());
TS_ASSERT_DIFFERS(c->getLocationId(), a->getLocationId());
TS_ASSERT_EQUALS(a->getLocationId(), a->getOtherLocation()->getLocationId());
TS_ASSERT_EQUALS(b->getLocationId(), b->getOtherLocation()->getLocationId());
TS_ASSERT_EQUALS(c->getLocationId(), c->getOtherLocation()->getLocationId());
}
void test_source_locations_have_right_file_path_line_column_and_token_id()
{
SourceLocationCollection collection;
SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 4, 5);
TS_ASSERT_EQUALS(1, a->getTokenIds()[0]);
TS_ASSERT_EQUALS(2, a->getLineNumber());
TS_ASSERT_EQUALS(3, a->getColumnNumber());
TS_ASSERT_EQUALS(4, a->getOtherLocation()->getLineNumber());
TS_ASSERT_EQUALS(5, a->getOtherLocation()->getColumnNumber());
TS_ASSERT_EQUALS("file.c", a->getFilePath().str());
}
void test_finding_source_locations_by_id()
{
SourceLocationCollection collection;
SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 4, 5);
SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {6}, "file.c", 7, 8, 9, 10);
TS_ASSERT_EQUALS(a, collection.getSourceLocationById(a->getLocationId()));
TS_ASSERT_EQUALS(b, collection.getSourceLocationById(b->getLocationId()));
}
void test_creating_plain_copy_of_all_locations_in_line_range()
{
SourceLocationCollection collection;
SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 2, 3, 4, 5);
SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {1}, "file.c", 3, 3, 4, 5);
SourceLocation* c = collection.addSourceLocation(LOCATION_TOKEN, 3, {1}, "file.c", 1, 3, 5, 5);
SourceLocation* d = collection.addSourceLocation(LOCATION_TOKEN, 4, {1}, "file.c", 1, 5, 4, 5);
Id ida = a->getLocationId();
Id idb = b->getLocationId();
Id idc = c->getLocationId();
Id idd = d->getLocationId();
unsigned int fromLine = 2;
unsigned int toLine = 4;
SourceLocationCollection copy;
SourceLocation* x = collection.getSourceLocationById(ida);
x->getSourceLocationFile()->forEachSourceLocation(
[&copy, fromLine, toLine](SourceLocation* location)
{
if (location->getLineNumber() >= fromLine && location->getLineNumber() <= toLine)
{
copy.addSourceLocationCopy(location);
}
}
);
TS_ASSERT_EQUALS(1, copy.getSourceLocationFileCount());
TS_ASSERT_EQUALS(3, copy.getSourceLocationCount());
TS_ASSERT(copy.getSourceLocationById(ida));
TS_ASSERT(copy.getSourceLocationById(idb));
TS_ASSERT(!copy.getSourceLocationById(idc));
TS_ASSERT(copy.getSourceLocationById(idd));
TS_ASSERT_DIFFERS(a, copy.getSourceLocationById(ida));
TS_ASSERT_DIFFERS(d, copy.getSourceLocationById(idd));
TS_ASSERT(copy.getSourceLocationById(ida)->getStartLocation());
TS_ASSERT(copy.getSourceLocationById(ida)->getEndLocation());
TS_ASSERT(!copy.getSourceLocationById(idd)->getStartLocation());
TS_ASSERT(copy.getSourceLocationById(idd)->getEndLocation());
}
void test_get_source_locations_filtered_by_lines()
{
SourceLocationCollection collection;
SourceLocation* a = collection.addSourceLocation(LOCATION_TOKEN, 1, {1}, "file.c", 1, 3, 1, 5);
SourceLocation* b = collection.addSourceLocation(LOCATION_TOKEN, 2, {1}, "file.c", 1, 3, 2, 5);
SourceLocation* c = collection.addSourceLocation(LOCATION_TOKEN, 3, {1}, "file.c", 2, 3, 2, 5);
SourceLocation* d = collection.addSourceLocation(LOCATION_TOKEN, 4, {1}, "file.c", 3, 3, 4, 5);
SourceLocation* e = collection.addSourceLocation(LOCATION_TOKEN, 5, {1}, "file.c", 3, 5, 5, 5);
SourceLocation* f = collection.addSourceLocation(LOCATION_TOKEN, 6, {1}, "file.c", 1, 5, 5, 5);
SourceLocation* g = collection.addSourceLocation(LOCATION_TOKEN, 7, {1}, "file.c", 5, 5, 5, 5);
SourceLocationCollection copy;
copy.addSourceLocationFile(
collection.getSourceLocationById(a->getLocationId())->getSourceLocationFile()->getFilteredByLines(2, 4));
TS_ASSERT_EQUALS(1, copy.getSourceLocationFileCount());
TS_ASSERT_EQUALS(4, copy.getSourceLocationCount());
TS_ASSERT(!copy.getSourceLocationById(a->getLocationId()));
TS_ASSERT(copy.getSourceLocationById(b->getLocationId()));
TS_ASSERT(copy.getSourceLocationById(c->getLocationId()));
TS_ASSERT(copy.getSourceLocationById(d->getLocationId()));
TS_ASSERT(copy.getSourceLocationById(e->getLocationId()));
TS_ASSERT(!copy.getSourceLocationById(f->getLocationId()));
TS_ASSERT(!copy.getSourceLocationById(g->getLocationId()));
TS_ASSERT_DIFFERS(b, copy.getSourceLocationById(b->getLocationId()));
TS_ASSERT_DIFFERS(c, copy.getSourceLocationById(c->getLocationId()));
TS_ASSERT(!copy.getSourceLocationById(b->getLocationId())->getStartLocation());
TS_ASSERT(copy.getSourceLocationById(b->getLocationId())->getEndLocation());
TS_ASSERT(copy.getSourceLocationById(e->getLocationId())->getStartLocation());
TS_ASSERT(!copy.getSourceLocationById(e->getLocationId())->getEndLocation());
}
};
-16
View File
@@ -5,7 +5,6 @@
#include "data/graph/token_component/TokenComponentAbstraction.h"
#include "data/graph/token_component/TokenComponentAccess.h"
#include "data/graph/token_component/TokenComponentStatic.h"
#include "data/location/TokenLocation.h"
#include "data/parser/ParseLocation.h"
#include "data/PersistentStorage.h"
@@ -238,13 +237,6 @@ private:
clear();
}
std::shared_ptr<TokenLocationCollection> getLocationCollectionForTokenId(Id id) const
{
std::vector<Id> tokenIds;
tokenIds.push_back(id);
return getTokenLocationsForTokenIds(tokenIds);
}
//const size_t getNodeCount() const
//{
// return getGraph().getNodeCount();
@@ -268,14 +260,6 @@ private:
return ParseLocation(m_filePath, 1, locationId, 1, locationId);
}
bool isValidLocation(TokenLocation* location, Id locationId) const
{
return
location->getFilePath() == m_filePath &&
location->getLineNumber() == 1 &&
location->getColumnNumber() == locationId;
}
NameHierarchy createFunctionNameHierarchy(std::string ret, std::string name, std::string parameters) const
{
NameHierarchy nameHierarchy = createNameHierarchy(name);
-162
View File
@@ -1,162 +0,0 @@
#include "cxxtest/TestSuite.h"
#include "data/location/TokenLocation.h"
#include "data/location/TokenLocationCollection.h"
#include "data/location/TokenLocationFile.h"
#include "data/location/TokenLocationLine.h"
class TokenLocationCollectionTestSuite : public CxxTest::TestSuite
{
public:
void test_token_locations_get_created_with_other_end()
{
TokenLocationCollection collection;
TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5);
TS_ASSERT(a);
TS_ASSERT(a->isStartTokenLocation());
TS_ASSERT(!a->isEndTokenLocation());
TokenLocation* b = a->getOtherTokenLocation();
TS_ASSERT(b);
TS_ASSERT(!b->isStartTokenLocation());
TS_ASSERT(b->isEndTokenLocation());
TS_ASSERT_EQUALS(a, b->getOtherTokenLocation());
TS_ASSERT_EQUALS(a, b->getStartTokenLocation());
TS_ASSERT_EQUALS(a, a->getStartTokenLocation());
TS_ASSERT_EQUALS(b, a->getEndTokenLocation());
TS_ASSERT_EQUALS(b, b->getEndTokenLocation());
}
void test_token_locations_do_not_get_created_with_wrong_input()
{
TokenLocationCollection collection;
TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 2, 1);
TokenLocation* b = collection.addTokenLocation(2, 1, "file.c", 4, 1, 1, 10);
TS_ASSERT(!a);
TS_ASSERT(!b);
}
void test_token_locations_get_unique_id_but_both_ends_have_the_same()
{
TokenLocationCollection collection;
TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 1, 1, 1, 1);
TokenLocation* b = collection.addTokenLocation(2, 2, "file.c", 1, 1, 1, 1);
TokenLocation* c = collection.addTokenLocation(3, 3, "file.c", 1, 1, 1, 1);
TS_ASSERT_EQUALS(1, collection.getTokenLocationFileCount());
TS_ASSERT_EQUALS(3, collection.getTokenLocationCount());
TS_ASSERT_DIFFERS(a->getId(), b->getId());
TS_ASSERT_DIFFERS(b->getId(), c->getId());
TS_ASSERT_DIFFERS(c->getId(), a->getId());
TS_ASSERT_EQUALS(a->getId(), a->getOtherTokenLocation()->getId());
TS_ASSERT_EQUALS(b->getId(), b->getOtherTokenLocation()->getId());
TS_ASSERT_EQUALS(c->getId(), c->getOtherTokenLocation()->getId());
}
void test_token_locations_have_right_file_path_line_column_and_token_id()
{
TokenLocationCollection collection;
TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5);
TS_ASSERT_EQUALS(1, a->getTokenId());
TS_ASSERT_EQUALS(2, a->getLineNumber());
TS_ASSERT_EQUALS(3, a->getColumnNumber());
TS_ASSERT_EQUALS(4, a->getOtherTokenLocation()->getLineNumber());
TS_ASSERT_EQUALS(5, a->getOtherTokenLocation()->getColumnNumber());
TS_ASSERT_EQUALS("file.c", a->getFilePath().str());
}
void test_finding_token_locations_by_id()
{
TokenLocationCollection collection;
TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5);
TokenLocation* b = collection.addTokenLocation(2, 6, "file.c", 7, 8, 9, 10);
TS_ASSERT_EQUALS(a, collection.findTokenLocationById(a->getId()));
TS_ASSERT_EQUALS(b, collection.findTokenLocationById(b->getId()));
}
void test_removing_token_locations()
{
TokenLocationCollection collection;
TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5);
TokenLocation* b = collection.addTokenLocation(2, 1, "file.c", 3, 3, 4, 5);
TokenLocation* c = collection.addTokenLocation(3, 1, "file.c", 1, 3, 5, 5);
TokenLocation* d = collection.addTokenLocation(4, 1, "file2.c", 1, 3, 5, 5);
TS_ASSERT_EQUALS(2, collection.getTokenLocationFileCount());
TS_ASSERT_EQUALS(4, collection.getTokenLocationCount());
TS_ASSERT_EQUALS(5, c->getTokenLocationFile()->getTokenLocationLineCount());
Id ida = a->getId();
Id idb = b->getId();
Id idc = c->getId();
collection.removeTokenLocation(a);
collection.removeTokenLocation(b->getOtherTokenLocation());
collection.removeTokenLocation(d);
TS_ASSERT(!collection.findTokenLocationById(ida));
TS_ASSERT(!collection.findTokenLocationById(idb));
TS_ASSERT_EQUALS(c, collection.findTokenLocationById(idc));
TS_ASSERT_EQUALS(1, collection.getTokenLocationFileCount());
TS_ASSERT_EQUALS(1, collection.getTokenLocationCount());
TS_ASSERT_EQUALS(2, c->getTokenLocationFile()->getTokenLocationLineCount());
}
void test_creating_plain_copy_of_all_locations_in_line_range()
{
TokenLocationCollection collection;
TokenLocation* a = collection.addTokenLocation(1, 1, "file.c", 2, 3, 4, 5);
TokenLocation* b = collection.addTokenLocation(2, 1, "file.c", 3, 3, 4, 5);
TokenLocation* c = collection.addTokenLocation(3, 1, "file.c", 1, 3, 5, 5);
TokenLocation* d = collection.addTokenLocation(4, 1, "file.c", 1, 5, 4, 5);
Id ida = a->getId();
Id idb = b->getId();
Id idc = c->getId();
Id idd = d->getId();
unsigned int fromLine = 2;
unsigned int toLine = 4;
TokenLocationCollection copy;
TokenLocation* x = collection.findTokenLocationById(ida);
x->getTokenLocationFile()->forEachTokenLocationLine([&copy, fromLine, toLine](TokenLocationLine* line)
{
unsigned int l = line->getLineNumber();
if (l >= fromLine && l <= toLine)
{
line->forEachTokenLocation([&copy](TokenLocation* location)
{
copy.addTokenLocationAsPlainCopy(location);
});
}
});
TS_ASSERT_EQUALS(1, copy.getTokenLocationFileCount());
TS_ASSERT_EQUALS(3, copy.getTokenLocationCount());
TS_ASSERT(copy.findTokenLocationById(ida));
TS_ASSERT(copy.findTokenLocationById(idb));
TS_ASSERT(!copy.findTokenLocationById(idc));
TS_ASSERT(copy.findTokenLocationById(idd));
TS_ASSERT_DIFFERS(a, copy.findTokenLocationById(ida));
TS_ASSERT_DIFFERS(d, copy.findTokenLocationById(idd));
TS_ASSERT(copy.findTokenLocationById(ida)->getStartTokenLocation());
TS_ASSERT(copy.findTokenLocationById(ida)->getEndTokenLocation());
TS_ASSERT(!copy.findTokenLocationById(idd)->getStartTokenLocation());
TS_ASSERT(copy.findTokenLocationById(idd)->getEndTokenLocation());
}
};