Skip to content

Commit 5f15ad9

Browse files
committed
Add API to get the date at which the unit file that produced SymbolLocation was modified
This allows us to check for out-of-date index entries.
1 parent bafe631 commit 5f15ad9

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

Sources/ISDBTestSupport/TestLocation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extension SymbolLocation {
5151
public init(_ loc: TestLocation, moduleName: String = TestLocation.unknownModuleName, isSystem: Bool = false) {
5252
self.init(
5353
path: loc.url.path,
54+
timestamp: Date(),
5455
moduleName: moduleName,
5556
isSystem: isSystem,
5657
line: loc.line,

Sources/IndexStoreDB/SymbolLocation.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212

1313
@_implementationOnly
1414
import CIndexStoreDB
15+
import Foundation
1516

1617
public struct SymbolLocation: Equatable {
1718
public var path: String
19+
/// The date at which the unit file that contains a symbol has last been modified.
20+
public var timestamp: Date
1821
public var moduleName: String
1922
public var isSystem: Bool
2023
public var line: Int
2124
public var utf8Column: Int
2225

23-
public init(path: String, moduleName: String, isSystem: Bool = false, line: Int, utf8Column: Int) {
26+
public init(path: String, timestamp: Date, moduleName: String, isSystem: Bool = false, line: Int, utf8Column: Int) {
2427
self.path = path
28+
self.timestamp = timestamp
2529
self.moduleName = moduleName
2630
self.isSystem = isSystem
2731
self.line = line
@@ -47,6 +51,7 @@ extension SymbolLocation: CustomStringConvertible {
4751
extension SymbolLocation {
4852
internal init(_ loc: indexstoredb_symbol_location_t) {
4953
path = String(cString: indexstoredb_symbol_location_path(loc))
54+
timestamp = Date(timeIntervalSince1970: indexstoredb_symbol_location_timestamp(loc))
5055
moduleName = String(cString: indexstoredb_symbol_location_module_name(loc))
5156
isSystem = indexstoredb_symbol_location_is_system(loc)
5257
line = Int(indexstoredb_symbol_location_line(loc))

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ INDEXSTOREDB_PUBLIC
375375
const char * _Nonnull
376376
indexstoredb_symbol_location_path(_Nonnull indexstoredb_symbol_location_t);
377377

378+
/// Returns a Unix timestamp (seconds since 1/1/1970) at which the unit file that contains a symbol has last been
379+
/// modified.
380+
INDEXSTOREDB_PUBLIC
381+
double
382+
indexstoredb_symbol_location_timestamp(_Nonnull indexstoredb_symbol_location_t loc);
383+
378384
/// Returns the module name of the given symbol location.
379385
///
380386
/// The string has the same lifetime as the \c indexstoredb_symbol_location_t.

lib/CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,17 @@ indexstoredb_symbol_location_path(indexstoredb_symbol_location_t loc) {
444444
return obj->getPath().getPathString().c_str();
445445
}
446446

447+
double
448+
indexstoredb_symbol_location_timestamp(indexstoredb_symbol_location_t loc) {
449+
auto obj = (SymbolLocation *)loc;
450+
// Up until C++20 the reference date of time_since_epoch is undefined but according to
451+
// https://en.cppreference.com/w/cpp/chrono/system_clock most implementations use Unix Time.
452+
// Since C++20, system_clock is defined to measure time since 1/1/1970.
453+
// We rely on `time_since_epoch` always returning the nanoseconds since 1/1/1970.
454+
auto nanosecondsSinceEpoch = obj->getPath().getModificationTime().time_since_epoch().count();
455+
return static_cast<double>(nanosecondsSinceEpoch) / 1000 / 1000 / 1000;
456+
}
457+
447458
const char *
448459
indexstoredb_symbol_location_module_name(indexstoredb_symbol_location_t loc) {
449460
auto obj = (SymbolLocation *)loc;

0 commit comments

Comments
 (0)