Bug Summary

File:tools/clang/tools/extra/clangd/Trace.cpp
Warning:line 101, column 11
Value stored to 'OriginTime' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Trace.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/tools/extra/clangd -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/extra/clangd -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/tools/extra/clangd -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/extra/clangd/Trace.cpp
1//===--- Trace.cpp - Performance tracing facilities -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Trace.h"
11#include "Context.h"
12#include "Function.h"
13#include "llvm/ADT/DenseSet.h"
14#include "llvm/ADT/ScopeExit.h"
15#include "llvm/Support/Chrono.h"
16#include "llvm/Support/FormatProviders.h"
17#include "llvm/Support/FormatVariadic.h"
18#include "llvm/Support/Threading.h"
19#include <atomic>
20#include <mutex>
21
22namespace clang {
23namespace clangd {
24namespace trace {
25using namespace llvm;
26
27namespace {
28// The current implementation is naive: each thread writes to Out guarded by Mu.
29// Perhaps we should replace this by something that disturbs performance less.
30class JSONTracer : public EventTracer {
31public:
32 JSONTracer(raw_ostream &Out, bool Pretty)
33 : Out(Out), Sep(""), Start(std::chrono::system_clock::now()),
34 JSONFormat(Pretty ? "{0:2}" : "{0}") {
35 // The displayTimeUnit must be ns to avoid low-precision overlap
36 // calculations!
37 Out << R"({"displayTimeUnit":"ns","traceEvents":[)"
38 << "\n";
39 rawEvent("M", json::obj{
40 {"name", "process_name"},
41 {"args", json::obj{{"name", "clangd"}}},
42 });
43 }
44
45 ~JSONTracer() {
46 Out << "\n]}";
47 Out.flush();
48 }
49
50 // We stash a Span object in the context. It will record the start/end,
51 // and this also allows us to look up the parent Span's information.
52 Context beginSpan(llvm::StringRef Name, json::obj *Args) override {
53 return Context::current().derive(
54 SpanKey, llvm::make_unique<JSONSpan>(this, Name, Args));
55 }
56
57 // Trace viewer requires each thread to properly stack events.
58 // So we need to mark only duration that the span was active on the thread.
59 // (Hopefully any off-thread activity will be connected by a flow event).
60 // Record the end time here, but don't write the event: Args aren't ready yet.
61 void endSpan() override {
62 Context::current().getExisting(SpanKey)->markEnded();
63 }
64
65 void instant(llvm::StringRef Name, json::obj &&Args) override {
66 captureThreadMetadata();
67 jsonEvent("i", json::obj{{"name", Name}, {"args", std::move(Args)}});
68 }
69
70 // Record an event on the current thread. ph, pid, tid, ts are set.
71 // Contents must be a list of the other JSON key/values.
72 void jsonEvent(StringRef Phase, json::obj &&Contents,
73 uint64_t TID = get_threadid(),
74 double Timestamp = 0) {
75 Contents["ts"] = Timestamp ? Timestamp : timestamp();
76 Contents["tid"] = TID;
77 std::lock_guard<std::mutex> Lock(Mu);
78 rawEvent(Phase, std::move(Contents));
79 }
80
81private:
82 class JSONSpan {
83 public:
84 JSONSpan(JSONTracer *Tracer, llvm::StringRef Name, json::obj *Args)
85 : StartTime(Tracer->timestamp()), EndTime(0), Name(Name),
86 TID(get_threadid()), Tracer(Tracer), Args(Args) {
87 // ~JSONSpan() may run in a different thread, so we need to capture now.
88 Tracer->captureThreadMetadata();
89
90 // We don't record begin events here (and end events in the destructor)
91 // because B/E pairs have to appear in the right order, which is awkward.
92 // Instead we send the complete (X) event in the destructor.
93
94 // If our parent was on a different thread, add an arrow to this span.
95 auto *Parent = Context::current().get(SpanKey);
96 if (Parent && *Parent && (*Parent)->TID != TID) {
97 // If the parent span ended already, then show this as "following" it.
98 // Otherwise show us as "parallel".
99 double OriginTime = (*Parent)->EndTime;
100 if (!OriginTime)
101 OriginTime = (*Parent)->StartTime;
Value stored to 'OriginTime' is never read
102
103 auto FlowID = nextID();
104 Tracer->jsonEvent("s",
105 json::obj{{"id", FlowID},
106 {"name", "Context crosses threads"},
107 {"cat", "dummy"}},
108 (*Parent)->TID, (*Parent)->StartTime);
109 Tracer->jsonEvent("f",
110 json::obj{{"id", FlowID},
111 {"bp", "e"},
112 {"name", "Context crosses threads"},
113 {"cat", "dummy"}},
114 TID);
115 }
116 }
117
118 ~JSONSpan() {
119 // Finally, record the event (ending at EndTime, not timestamp())!
120 Tracer->jsonEvent("X",
121 json::obj{{"name", std::move(Name)},
122 {"args", std::move(*Args)},
123 {"dur", EndTime - StartTime}},
124 TID, StartTime);
125 }
126
127 // May be called by any thread.
128 void markEnded() {
129 EndTime = Tracer->timestamp();
130 }
131
132 private:
133 static uint64_t nextID() {
134 static std::atomic<uint64_t> Next = {0};
135 return Next++;
136 }
137
138 double StartTime;
139 std::atomic<double> EndTime; // Filled in by markEnded().
140 std::string Name;
141 uint64_t TID;
142 JSONTracer *Tracer;
143 json::obj *Args;
144 };
145 static Key<std::unique_ptr<JSONSpan>> SpanKey;
146
147 // Record an event. ph and pid are set.
148 // Contents must be a list of the other JSON key/values.
149 void rawEvent(StringRef Phase, json::obj &&Event) /*REQUIRES(Mu)*/ {
150 // PID 0 represents the clangd process.
151 Event["pid"] = 0;
152 Event["ph"] = Phase;
153 Out << Sep << formatv(JSONFormat, json::Expr(std::move(Event)));
154 Sep = ",\n";
155 }
156
157 // If we haven't already, emit metadata describing this thread.
158 void captureThreadMetadata() {
159 uint64_t TID = get_threadid();
160 std::lock_guard<std::mutex> Lock(Mu);
161 if (ThreadsWithMD.insert(TID).second) {
162 SmallString<32> Name;
163 get_thread_name(Name);
164 if (!Name.empty()) {
165 rawEvent("M", json::obj{
166 {"tid", TID},
167 {"name", "thread_name"},
168 {"args", json::obj{{"name", Name}}},
169 });
170 }
171 }
172 }
173
174 double timestamp() {
175 using namespace std::chrono;
176 return duration<double, std::micro>(system_clock::now() - Start).count();
177 }
178
179 std::mutex Mu;
180 raw_ostream &Out /*GUARDED_BY(Mu)*/;
181 const char *Sep /*GUARDED_BY(Mu)*/;
182 DenseSet<uint64_t> ThreadsWithMD /*GUARDED_BY(Mu)*/;
183 const sys::TimePoint<> Start;
184 const char *JSONFormat;
185};
186
187Key<std::unique_ptr<JSONTracer::JSONSpan>> JSONTracer::SpanKey;
188
189EventTracer *T = nullptr;
190} // namespace
191
192Session::Session(EventTracer &Tracer) {
193 assert(!T && "Resetting global tracer is not allowed.")(static_cast <bool> (!T && "Resetting global tracer is not allowed."
) ? void (0) : __assert_fail ("!T && \"Resetting global tracer is not allowed.\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/extra/clangd/Trace.cpp"
, 193, __extension__ __PRETTY_FUNCTION__))
;
194 T = &Tracer;
195}
196
197Session::~Session() { T = nullptr; }
198
199std::unique_ptr<EventTracer> createJSONTracer(llvm::raw_ostream &OS,
200 bool Pretty) {
201 return llvm::make_unique<JSONTracer>(OS, Pretty);
202}
203
204void log(const Twine &Message) {
205 if (!T)
206 return;
207 T->instant("Log", json::obj{{"Message", Message.str()}});
208}
209
210// Returned context owns Args.
211static Context makeSpanContext(llvm::Twine Name, json::obj *Args) {
212 if (!T)
213 return Context::current().clone();
214 WithContextValue WithArgs{std::unique_ptr<json::obj>(Args)};
215 return T->beginSpan(Name.isSingleStringRef() ? Name.getSingleStringRef()
216 : llvm::StringRef(Name.str()),
217 Args);
218}
219
220// Span keeps a non-owning pointer to the args, which is how users access them.
221// The args are owned by the context though. They stick around until the
222// beginSpan() context is destroyed, when the tracing engine will consume them.
223Span::Span(llvm::Twine Name)
224 : Args(T ? new json::obj() : nullptr),
225 RestoreCtx(makeSpanContext(Name, Args)) {}
226
227Span::~Span() {
228 if (T)
229 T->endSpan();
230}
231
232} // namespace trace
233} // namespace clangd
234} // namespace clang