Skip to content

Commit d1f46c3

Browse files
committed
[refactor, WIP] Added docs for Location-related entities.
1 parent 88c3be1 commit d1f46c3

10 files changed

Lines changed: 108 additions & 67 deletions

File tree

include/klee/Module/LocationInfo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class Instruction;
2222
class Module;
2323
} // namespace llvm
2424

25+
namespace klee {
26+
struct PhysicalLocationJson;
27+
}
28+
2529
namespace klee {
2630

2731
/// @brief Immutable struct representing location in source code.
@@ -34,6 +38,12 @@ struct LocationInfo {
3438

3539
/// @brief Column number in source file.
3640
const std::optional<uint64_t> column;
41+
42+
/// @brief Converts location info to SARIFs representation
43+
/// of location.
44+
/// @param location location info in source code.
45+
/// @return SARIFs representation of location.
46+
PhysicalLocationJson serialize() const;
3747
};
3848

3949
LocationInfo getLocationInfo(const llvm::Function *func);

lib/Core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ add_library(kleeCore
1515
CoreStats.cpp
1616
CXXTypeSystem/CXXTypeManager.cpp
1717
DistanceCalculator.cpp
18-
EventList.cpp
18+
EventRecorder.cpp
1919
ExecutionState.cpp
2020
Executor.cpp
2121
ExecutorUtil.cpp

lib/Core/CodeEvent.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ class CodeEvent {
2929
: kind(kind), location(location) {}
3030

3131
/// @brief Additional info to current event.
32-
/// @returns string describing event.
32+
/// @return String describing event.
3333
virtual std::string description() const = 0;
3434

35-
// FIXME: compelte the docs.
36-
/// @brief
37-
/// @param event
38-
/// @return
35+
/// @brief Serialize event to the JSON format.
36+
/// @return JSON object describing event.
3937
LocationJson serialize() const {
4038
LocationJson result;
4139

@@ -45,6 +43,8 @@ class CodeEvent {
4543
return result;
4644
}
4745

46+
/// @brief Kind of event used for LLVM RTTI.
47+
/// @return Kind of event.
4848
EventKind getKind() const { return kind; }
4949

5050
virtual ~CodeEvent() = default;

lib/Core/CodeLocation.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
#include "CodeLocation.h"
22

3+
#include "klee/ADT/Ref.h"
34
#include "klee/Module/LocationInfo.h"
45
#include "klee/Module/SarifReport.h"
56

7+
#include <cinttypes>
68
#include <optional>
9+
#include <string>
710
#include <utility>
811

912
using namespace klee;
1013

11-
/// @brief Converts location info to SARIFs representation
12-
/// of location.
13-
/// @param location location info in source code.
14-
/// @return SARIFs representation of location.
15-
static PhysicalLocationJson from(const LocationInfo &location) {
16-
// Clang-format does not keep pretty format for JSON.
17-
// For better readability split creating of JSON in local variables.
18-
19-
ArtifactLocationJson artifactLocationJson{{location.file}};
20-
RegionJson regionJson{
21-
{location.line}, std::nullopt, location.column, std::nullopt};
22-
PhysicalLocationJson physicalLocationJson{{std::move(artifactLocationJson)},
23-
{std::move(regionJson)}};
14+
CodeLocation::CodeLocation(const Path::PathIndex &pathIndex,
15+
const KValue *source,
16+
const std::string &sourceFilename,
17+
uint64_t sourceCodeLine,
18+
std::optional<uint64_t> sourceCodeColumn)
19+
: pathIndex(pathIndex), source(source),
20+
location(LocationInfo{sourceFilename, sourceCodeLine, sourceCodeColumn}) {
21+
}
2422

25-
return physicalLocationJson;
23+
ref<CodeLocation>
24+
CodeLocation::create(const Path::PathIndex &pathIndex, const KValue *source,
25+
const std::string &sourceFilename, uint64_t sourceCodeLine,
26+
std::optional<uint64_t> sourceCodeColumn = std::nullopt) {
27+
return new CodeLocation(pathIndex, source, sourceFilename, sourceCodeLine,
28+
sourceCodeColumn);
2629
}
2730

28-
////////////////////////////////////////////////////////////////
31+
ref<CodeLocation>
32+
CodeLocation::create(const KValue *source, const std::string &sourceFilename,
33+
uint64_t sourceCodeLine,
34+
std::optional<uint64_t> sourceCodeColumn = std::nullopt) {
35+
return new CodeLocation(Path::PathIndex{0, 0}, source, sourceFilename,
36+
sourceCodeLine, sourceCodeColumn);
37+
}
2938

30-
PhysicalLocationJson CodeLocation::serialize() const { return from(location); }
39+
PhysicalLocationJson CodeLocation::serialize() const {
40+
return location.serialize();
41+
}

lib/Core/CodeLocation.h

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "klee/Expr/Path.h"
66
#include "klee/Module/LocationInfo.h"
77

8+
#include <cinttypes>
89
#include <cstdint>
910
#include <iostream>
1011
#include <optional>
@@ -33,41 +34,35 @@ struct CodeLocation {
3334
private:
3435
CodeLocation(const Path::PathIndex &pathIndex, const KValue *source,
3536
const std::string &sourceFilename, uint64_t sourceCodeLine,
36-
std::optional<uint64_t> sourceCodeColumn)
37-
: pathIndex(pathIndex), source(source),
38-
location(
39-
LocationInfo{sourceFilename, sourceCodeLine, sourceCodeColumn}) {}
37+
std::optional<uint64_t> sourceCodeColumn);
4038

4139
CodeLocation(const CodeLocation &) = delete;
4240
CodeLocation &operator=(const CodeLocation &) = delete;
4341

4442
public:
45-
/// @brief: Factory method. Wraps constructed objects in the ref
46-
/// to provide zero-cost copying of code locations.
47-
/// @param sourceFilename
48-
/// @param sourceCodeLine
49-
/// @param sourceCodeColumn
50-
/// @return
51-
static ref<CodeLocation>
52-
create(const Path::PathIndex &pathIndex, const KValue *source,
53-
const std::string &sourceFilename, uint64_t sourceCodeLine,
54-
std::optional<uint64_t> sourceCodeColumn = std::nullopt) {
55-
return new CodeLocation(pathIndex, source, sourceFilename, sourceCodeLine,
56-
sourceCodeColumn);
57-
}
43+
/// @brief Factory method for `CodeLocation` enhanced with `PathIndex`
44+
/// in history. Wraps constructed objects in the ref to provide
45+
/// zero-cost copying of code locations.
46+
/// @param sourceFilename Name of source file to which location refers.
47+
/// @param sourceCodeLine Line in source to which location refers.
48+
/// @param sourceCodeColumn Column in source code to which location refers.
49+
/// @return `CodeLocation` representing the location in source code.
50+
static ref<CodeLocation> create(const Path::PathIndex &pathIndex,
51+
const KValue *source,
52+
const std::string &sourceFilename,
53+
uint64_t sourceCodeLine,
54+
std::optional<uint64_t> sourceCodeColumn);
5855

59-
/// @brief TODO: finish docs.
60-
/// @param sourceFilename
61-
/// @param sourceCodeLine
62-
/// @param sourceCodeColumn
63-
/// @return
64-
static ref<CodeLocation>
65-
create(const KValue *source, const std::string &sourceFilename,
66-
uint64_t sourceCodeLine,
67-
std::optional<uint64_t> sourceCodeColumn = std::nullopt) {
68-
return new CodeLocation(Path::PathIndex{0, 0}, source, sourceFilename,
69-
sourceCodeLine, sourceCodeColumn);
70-
}
56+
/// @brief Factory method for `CodeLocation`. Wraps constructed
57+
/// objects in the ref to provide zero-cost copying of code locations.
58+
/// @param sourceFilename Name of source file to which location refers.
59+
/// @param sourceCodeLine Line in source to which location refers.
60+
/// @param sourceCodeColumn Column in source code to which location refers.
61+
/// @return `CodeLocation` representing the location in source code.
62+
static ref<CodeLocation> create(const KValue *source,
63+
const std::string &sourceFilename,
64+
uint64_t sourceCodeLine,
65+
std::optional<uint64_t> sourceCodeColumn);
7166

7267
/// @brief Converts code location info to SARIFs representation
7368
/// of location.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "EventList.h"
1+
#include "EventRecorder.h"
22
#include "CodeEvent.h"
33

44
#include "klee/ADT/ImmutableList.h"
@@ -60,6 +60,7 @@ EventRecorder EventRecorder::inRange(const Path::PathIndex &begin,
6060
}
6161

6262
// TODO: not effective.
63+
/// FIXME:
6364
for (const auto &event : events) {
6465
const Path::PathIndex &eventPathIndex = event->location->pathIndex;
6566
if (!Path::PathIndexCompare{}(eventPathIndex, begin) &&
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef KLEE_EVENT_LIST_H
2-
#define KLEE_EVENT_LIST_H
1+
#ifndef KLEE_EVENT_RECORDER_H
2+
#define KLEE_EVENT_RECORDER_H
33

44
#include "klee/Expr/Path.h"
55

@@ -28,36 +28,37 @@ class EventRecorder {
2828
/// @param event event to record.
2929
void record(const ref<CodeEvent> &event);
3030

31-
/// @brief
32-
/// @param rhs
31+
/// @brief Appends all events from the given `EventRecorder`
32+
/// to this recorder.
33+
/// @param rhs `EventRecorder` to get events from.
3334
void append(const EventRecorder &rhs);
3435

3536
/// @brief Returns events from the history in specified range.
3637
/// @param begin range begin.
3738
/// @param end range end.
38-
/// @return immutable list containing recorder events from inner storage.
39+
/// @return `EventRecorder` containing recorder events from inner storage.
3940
EventRecorder inRange(const Path::PathIndex &begin,
4041
const Path::PathIndex &end) const;
4142

4243
/// @brief Returns events from the history from the givent event.
4344
/// @param begin range begin.
44-
/// @return immutable list containing recorder events from inner storage.
45+
/// @return `EventRecorder` containing recorder events from inner storage.
4546
EventRecorder tail(const Path::PathIndex &begin) const;
4647

47-
/// @brief
48-
/// @return
48+
/// @brief Returns last recorder event.
49+
/// @return Last recorded `CodeEvent`.
4950
ref<CodeEvent> last() const;
5051

51-
/// @brief
52-
/// @return
52+
/// @brief Tests whether this event recorder is empty.
53+
/// @return `true` if there are no events have been recorded.
5354
bool empty() const;
5455

55-
/// @brief Serializes events in this recorder to SARIF report.
56+
/// @brief Serializes events in this recorder to SARIF format.
5657
/// @return Structure ready for wrapping into json
5758
/// (i.e. `json(serialize())`) and serialization.
5859
CodeFlowJson serialize() const;
5960
};
6061

6162
} // namespace klee
6263

63-
#endif // KLEE_EVENT_LIST_H
64+
#endif // KLEE_EVENT_RECORDER_H

lib/Core/ExecutionState.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "klee/Utilities/Math.h"
3333

3434
#include "CodeLocation.h"
35-
#include "EventList.h"
35+
#include "EventRecorder.h"
3636

3737
#include "klee/Support/CompilerWarning.h"
3838
DISABLE_WARNING_PUSH

lib/Core/Executor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
#include "CodeEvents/ErrorEvent.h"
7979
#include "CodeEvents/ReturnEvent.h"
8080
#include "CodeLocation.h"
81-
#include "EventList.h"
81+
#include "EventRecorder.h"
8282

8383
#include "klee/Support/CompilerWarning.h"
8484
DISABLE_WARNING_PUSH

lib/Module/LocationInfo.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "klee/Module/LocationInfo.h"
11-
#include "klee/Support/CompilerWarning.h"
11+
#include "klee/Module/SarifReport.h"
1212

13+
#include "klee/Support/CompilerWarning.h"
1314
DISABLE_WARNING_PUSH
1415
DISABLE_WARNING_DEPRECATED_DECLARATIONS
1516
#include "llvm/ADT/SmallVector.h"
@@ -28,6 +29,28 @@ DISABLE_WARNING_POP
2829

2930
namespace klee {
3031

32+
PhysicalLocationJson LocationInfo::serialize() const {
33+
// clang-format off
34+
return PhysicalLocationJson{
35+
{
36+
ArtifactLocationJson {
37+
{file}
38+
}
39+
},
40+
{
41+
RegionJson {
42+
{line},
43+
std::nullopt,
44+
column,
45+
std::nullopt
46+
}
47+
}
48+
};
49+
// clang-format on
50+
}
51+
52+
////////////////////////////////////////////////////////////////
53+
3154
LocationInfo getLocationInfo(const llvm::Function *func) {
3255
const auto dsub = func->getSubprogram();
3356

0 commit comments

Comments
 (0)