-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputPipeline.cpp
More file actions
1444 lines (1300 loc) · 57.3 KB
/
InputPipeline.cpp
File metadata and controls
1444 lines (1300 loc) · 57.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
#include "analysis/InputPipeline.hpp"
#include "analysis/CompileCommands.hpp"
#include "analysis/FrontendDiagnostics.hpp"
#include "analyzer/HotspotProfiler.hpp"
#include <algorithm>
#include <cctype>
#include <chrono>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <mutex>
#include <optional>
#include <sstream>
#include <system_error>
#include <unordered_set>
#include <vector>
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/Error.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/JSON.h>
#include <llvm/Support/MD5.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/raw_ostream.h>
#include <compilerlib/compiler.h>
#include <coretrace/logger.hpp>
namespace ctrace::stack::analysis
{
namespace
{
std::mutex gCompileWorkingDirMutex;
std::string makeAbsolutePath(const std::string& path)
{
std::error_code ec;
std::filesystem::path absPath = std::filesystem::absolute(path, ec);
if (ec)
return path;
return absPath.lexically_normal().generic_string();
}
void appendIfMissing(std::vector<std::string>& args, const std::string& flag)
{
if (std::find(args.begin(), args.end(), flag) == args.end())
args.push_back(flag);
}
static void removeOutputPathArgs(std::vector<std::string>& args)
{
std::vector<std::string> filtered;
filtered.reserve(args.size());
for (std::size_t i = 0; i < args.size(); ++i)
{
const std::string& arg = args[i];
if (arg == "-o")
{
if (i + 1 < args.size())
++i;
continue;
}
if (arg.rfind("-o=", 0) == 0)
continue;
if (arg.size() > 2 && arg.rfind("-o", 0) == 0)
continue;
filtered.push_back(arg);
}
args.swap(filtered);
}
static std::vector<std::string>
buildBitcodeCompileArgs(const std::vector<std::string>& baseArgs,
const std::filesystem::path& outputBitcodePath)
{
std::vector<std::string> args = baseArgs;
args.erase(std::remove(args.begin(), args.end(), "-S"), args.end());
removeOutputPathArgs(args);
appendIfMissing(args, "-emit-llvm");
appendIfMissing(args, "-c");
args.push_back("-o");
args.push_back(outputBitcodePath.string());
return args;
}
bool hasDebugFlag(const std::vector<std::string>& args)
{
for (const auto& arg : args)
{
if (arg == "-g" || (arg.size() > 2 && arg.rfind("-g", 0) == 0))
return true;
}
return false;
}
void applyCompdbFastMode(std::vector<std::string>& args)
{
std::vector<std::string> filtered;
filtered.reserve(args.size());
for (std::size_t i = 0; i < args.size(); ++i)
{
const auto& arg = args[i];
auto skipFlagWithValue = [&](const std::string& flag) -> bool
{
if (arg != flag)
return false;
if (i + 1 < args.size())
++i;
return true;
};
if (arg.size() > 1 && arg.rfind("-O", 0) == 0)
continue;
if (arg.rfind("-g", 0) == 0)
continue;
if (arg.rfind("-fsanitize", 0) == 0 || arg.rfind("-fno-sanitize", 0) == 0)
continue;
if (arg == "-flto" || arg.rfind("-flto=", 0) == 0)
continue;
if (arg.rfind("-fprofile", 0) == 0 || arg.rfind("-fcoverage", 0) == 0)
continue;
// Cross-platform portability: drop Apple-specific compile flags that
// commonly appear in macOS compile databases and fail on Linux CI.
if (skipFlagWithValue("-arch"))
continue;
if (arg.rfind("-arch=", 0) == 0)
continue;
if (skipFlagWithValue("-isysroot"))
continue;
if (arg.rfind("-isysroot=", 0) == 0)
continue;
if (skipFlagWithValue("-iframework"))
continue;
if (arg.rfind("-mmacosx-version-min", 0) == 0)
continue;
if (arg.rfind("-miphoneos-version-min", 0) == 0)
continue;
if (arg.rfind("-mios-simulator-version-min", 0) == 0)
continue;
if (arg == "-fembed-bitcode" || arg == "-fapplication-extension")
continue;
if (arg == "-target" && i + 1 < args.size())
{
const std::string& triple = args[i + 1];
if (triple.find("apple") != std::string::npos)
{
++i;
continue;
}
}
if (arg.rfind("--target=", 0) == 0 &&
arg.find("apple", std::string("--target=").size()) != std::string::npos)
{
continue;
}
filtered.push_back(arg);
}
filtered.push_back("-O0");
filtered.push_back("-gline-tables-only");
filtered.push_back("-fno-sanitize=all");
args.swap(filtered);
}
static void logText(coretrace::Level level, const std::string& text)
{
if (text.empty())
return;
if (text.back() == '\n')
{
coretrace::log(level, "{}", text);
}
else
{
coretrace::log(level, "{}\n", text);
}
}
constexpr llvm::StringLiteral kCompileIRCacheSchema = "compile-ir-cache-v2";
struct FileSnapshot
{
std::string path;
std::uint64_t size = 0;
std::int64_t mtimeNs = 0;
};
struct CompileIRCachePaths
{
std::filesystem::path directory;
std::filesystem::path metaFile;
std::filesystem::path bcFile;
std::filesystem::path irFile;
std::filesystem::path depFile;
std::uint64_t enabled : 1 = false;
std::uint64_t reservedFlags : 63 = 0;
};
struct CompileIRCachePayload
{
std::string llvmBitcode;
std::string llvmIR;
std::string diagnostics;
};
static std::string md5Hex(llvm::StringRef input)
{
llvm::MD5 hasher;
hasher.update(input);
llvm::MD5::MD5Result out;
hasher.final(out);
llvm::SmallString<32> hex;
llvm::MD5::stringifyResult(out, hex);
return std::string(hex.str());
}
static std::string makeAbsolutePathFrom(const std::string& path, const std::string& baseDir)
{
std::filesystem::path p(path);
if (p.is_relative() && !baseDir.empty())
p = std::filesystem::path(baseDir) / p;
std::error_code ec;
std::filesystem::path absPath = std::filesystem::absolute(p, ec);
if (ec)
return p.lexically_normal().generic_string();
return absPath.lexically_normal().generic_string();
}
static std::optional<FileSnapshot> captureFileSnapshot(const std::string& path)
{
if (path.empty())
return std::nullopt;
std::error_code ec;
std::filesystem::path absolute = std::filesystem::absolute(path, ec);
if (ec)
return std::nullopt;
absolute = absolute.lexically_normal();
if (!std::filesystem::exists(absolute, ec) || ec)
return std::nullopt;
if (!std::filesystem::is_regular_file(absolute, ec) || ec)
return std::nullopt;
const auto size = std::filesystem::file_size(absolute, ec);
if (ec)
return std::nullopt;
const auto mtime = std::filesystem::last_write_time(absolute, ec);
if (ec)
return std::nullopt;
const auto mtimeNs = std::chrono::time_point_cast<std::chrono::nanoseconds>(mtime)
.time_since_epoch()
.count();
FileSnapshot snapshot;
snapshot.path = absolute.generic_string();
snapshot.size = static_cast<std::uint64_t>(size);
snapshot.mtimeNs = static_cast<std::int64_t>(mtimeNs);
return snapshot;
}
static bool isSnapshotCurrent(const FileSnapshot& expected)
{
const auto current = captureFileSnapshot(expected.path);
if (!current)
return false;
return current->size == expected.size && current->mtimeNs == expected.mtimeNs;
}
static llvm::json::Object encodeSnapshot(const FileSnapshot& snapshot)
{
llvm::json::Object obj;
obj["path"] = snapshot.path;
obj["size"] = static_cast<std::int64_t>(snapshot.size);
obj["mtimeNs"] = snapshot.mtimeNs;
return obj;
}
static std::optional<FileSnapshot> decodeSnapshot(const llvm::json::Value& value)
{
const auto* obj = value.getAsObject();
if (!obj)
return std::nullopt;
const auto path = obj->getString("path");
const auto size = obj->getInteger("size");
const auto mtimeNs = obj->getInteger("mtimeNs");
if (!path || !size || !mtimeNs || *size < 0)
return std::nullopt;
FileSnapshot snapshot;
snapshot.path = path->str();
snapshot.size = static_cast<std::uint64_t>(*size);
snapshot.mtimeNs = static_cast<std::int64_t>(*mtimeNs);
return snapshot;
}
static bool readTextFile(const std::filesystem::path& path, std::string& out)
{
out.clear();
std::ifstream in(path, std::ios::in | std::ios::binary);
if (!in)
return false;
std::ostringstream buffer;
buffer << in.rdbuf();
out = buffer.str();
return true;
}
static bool writeTextFile(const std::filesystem::path& path, const std::string& content)
{
std::ofstream out(path, std::ios::out | std::ios::trunc | std::ios::binary);
if (!out)
return false;
out << content;
return out.good();
}
static CompileIRCachePaths buildCompileIRCachePaths(const AnalysisConfig& config,
const std::string& filename,
LanguageType language,
const std::vector<std::string>& args,
const std::string& workingDir)
{
CompileIRCachePaths paths;
if (config.compileIRCacheDir.empty())
return paths;
std::ostringstream keyPayload;
keyPayload << std::string(kCompileIRCacheSchema) << "\n";
keyPayload << "language:" << static_cast<int>(language) << "\n";
keyPayload << "compileIRFormat:"
<< (config.compileIRFormat == CompileIRFormat::LL ? "ll" : "bc") << "\n";
keyPayload << "file:" << makeAbsolutePathFrom(filename, workingDir) << "\n";
keyPayload << "workingDir:" << makeAbsolutePathFrom(workingDir, "") << "\n";
for (const std::string& arg : args)
keyPayload << "arg:" << arg << "\n";
const std::string key = md5Hex(keyPayload.str());
std::filesystem::path directory = config.compileIRCacheDir;
paths.enabled = true;
paths.directory = directory;
paths.metaFile = directory / (key + ".json");
paths.bcFile = directory / (key + ".bc");
paths.irFile = directory / (key + ".ll");
paths.depFile = directory / (key + ".d");
return paths;
}
static std::optional<std::vector<std::string>>
parseDepfileDependencies(const std::filesystem::path& depFile,
const std::string& compileWorkingDir)
{
std::string content;
if (!readTextFile(depFile, content))
return std::nullopt;
std::string merged;
merged.reserve(content.size());
for (std::size_t i = 0; i < content.size(); ++i)
{
const char c = content[i];
if (c == '\\' && i + 1 < content.size())
{
if (content[i + 1] == '\n')
{
++i;
continue;
}
if (content[i + 1] == '\r' && i + 2 < content.size() && content[i + 2] == '\n')
{
i += 2;
continue;
}
}
merged.push_back(c);
}
std::size_t colonPos = std::string::npos;
for (std::size_t i = 0; i < merged.size(); ++i)
{
if (merged[i] != ':')
continue;
if (i + 1 < merged.size() &&
std::isspace(static_cast<unsigned char>(merged[i + 1])))
{
colonPos = i;
break;
}
}
if (colonPos == std::string::npos || colonPos + 1 >= merged.size())
return std::nullopt;
const std::string depsPart = merged.substr(colonPos + 1);
std::vector<std::string> dependencies;
std::unordered_set<std::string> seen;
std::string current;
bool escaping = false;
auto flushDependency = [&]()
{
if (current.empty())
return;
const std::string absolute = makeAbsolutePathFrom(current, compileWorkingDir);
if (!absolute.empty() && seen.insert(absolute).second)
dependencies.push_back(absolute);
current.clear();
};
for (char ch : depsPart)
{
if (escaping)
{
current.push_back(ch);
escaping = false;
continue;
}
if (ch == '\\')
{
escaping = true;
continue;
}
if (std::isspace(static_cast<unsigned char>(ch)))
{
flushDependency();
continue;
}
current.push_back(ch);
}
flushDependency();
if (dependencies.empty())
return std::nullopt;
return dependencies;
}
static bool ensureDirectoryExists(const std::filesystem::path& directory)
{
std::error_code ec;
std::filesystem::create_directories(directory, ec);
return !ec;
}
static std::optional<CompileIRCachePayload>
loadCompileIRCachePayload(const CompileIRCachePaths& cachePaths)
{
if (!cachePaths.enabled)
return std::nullopt;
std::string metadataText;
if (!readTextFile(cachePaths.metaFile, metadataText))
return std::nullopt;
auto parsed = llvm::json::parse(metadataText);
if (!parsed)
return std::nullopt;
const auto* root = parsed->getAsObject();
if (!root)
return std::nullopt;
const auto schema = root->getString("schema");
if (!schema || *schema != kCompileIRCacheSchema)
return std::nullopt;
const auto* sourceValue = root->get("source");
if (!sourceValue)
return std::nullopt;
const auto sourceSnapshot = decodeSnapshot(*sourceValue);
if (!sourceSnapshot || !isSnapshotCurrent(*sourceSnapshot))
return std::nullopt;
const auto* depsArray = root->getArray("dependencies");
if (!depsArray)
return std::nullopt;
for (const auto& depValue : *depsArray)
{
const auto depSnapshot = decodeSnapshot(depValue);
if (!depSnapshot || !isSnapshotCurrent(*depSnapshot))
return std::nullopt;
}
std::string llvmBitcode;
(void)readTextFile(cachePaths.bcFile, llvmBitcode);
std::string llvmIR;
(void)readTextFile(cachePaths.irFile, llvmIR);
if (llvmBitcode.empty() && llvmIR.empty())
return std::nullopt;
CompileIRCachePayload payload;
payload.llvmBitcode = std::move(llvmBitcode);
payload.llvmIR = std::move(llvmIR);
if (const auto diagnostics = root->getString("diagnostics"))
payload.diagnostics = diagnostics->str();
return payload;
}
static bool storeCompileIRCachePayload(const CompileIRCachePaths& cachePaths,
const FileSnapshot& sourceSnapshot,
const std::vector<FileSnapshot>& dependencySnapshots,
const std::string& diagnostics,
const std::string& llvmIR,
const std::string& llvmBitcode)
{
if (!cachePaths.enabled)
return false;
if (dependencySnapshots.empty())
return false;
if (!ensureDirectoryExists(cachePaths.directory))
return false;
llvm::json::Array dependenciesArray;
for (const FileSnapshot& dependency : dependencySnapshots)
dependenciesArray.push_back(encodeSnapshot(dependency));
llvm::json::Object root;
root["schema"] = kCompileIRCacheSchema;
root["source"] = encodeSnapshot(sourceSnapshot);
root["dependencies"] = std::move(dependenciesArray);
root["diagnostics"] = diagnostics;
std::string metadataText;
llvm::raw_string_ostream metadataStream(metadataText);
metadataStream << llvm::formatv("{0:2}", llvm::json::Value(std::move(root)));
metadataStream.flush();
if (!llvmBitcode.empty() && !writeTextFile(cachePaths.bcFile, llvmBitcode))
return false;
if (!writeTextFile(cachePaths.irFile, llvmIR))
return false;
if (!writeTextFile(cachePaths.metaFile, metadataText))
return false;
return true;
}
static std::optional<std::vector<FileSnapshot>>
buildDependencySnapshots(const std::vector<std::string>& dependencies)
{
std::vector<FileSnapshot> snapshots;
snapshots.reserve(dependencies.size());
for (const std::string& dependencyPath : dependencies)
{
const auto snapshot = captureFileSnapshot(dependencyPath);
if (!snapshot)
return std::nullopt;
snapshots.push_back(*snapshot);
}
return snapshots;
}
static void appendDependencyCaptureArgs(std::vector<std::string>& args,
const std::filesystem::path& depFile)
{
args.push_back("-MMD");
args.push_back("-MF");
args.push_back(depFile.string());
args.push_back("-MT");
args.push_back("coretrace_compile_ir_cache_target");
}
static bool resolveDumpIRPath(const AnalysisConfig& config, const std::string& inputPath,
const std::filesystem::path& baseDir,
std::filesystem::path& outPath, std::string& error)
{
if (config.dumpIRPath.empty())
return false;
std::filesystem::path dumpPath(config.dumpIRPath);
if (dumpPath.is_relative() && !baseDir.empty())
dumpPath = baseDir / dumpPath;
if (config.dumpIRIsDir)
{
std::filesystem::path baseName = std::filesystem::path(inputPath).filename();
std::string outName = baseName.empty() ? "module" : baseName.string();
outPath = dumpPath / (outName + ".ll");
}
else
{
outPath = dumpPath;
}
std::filesystem::path parentDir = outPath.parent_path();
if (!parentDir.empty())
{
std::error_code ec;
std::filesystem::create_directories(parentDir, ec);
if (ec)
{
error = "Failed to create IR dump directory: " + parentDir.string();
return false;
}
}
std::error_code absErr;
std::filesystem::path inputAbs = std::filesystem::absolute(inputPath, absErr);
std::filesystem::path outputAbs = std::filesystem::absolute(outPath, absErr);
if (!absErr && inputAbs == outputAbs)
{
error =
"Refusing to overwrite input file with --dump-ir output: " + outPath.string();
return false;
}
return true;
}
static bool dumpModuleIR(const llvm::Module& module, const std::string& inputPath,
const AnalysisConfig& config, const std::filesystem::path& baseDir,
std::string& error)
{
if (config.dumpIRPath.empty())
return true;
std::filesystem::path outPath;
if (!resolveDumpIRPath(config, inputPath, baseDir, outPath, error))
return false;
std::error_code ec;
llvm::raw_fd_ostream os(outPath.string(), ec, llvm::sys::fs::OF_Text);
if (ec)
{
error =
"Failed to write IR dump file: " + outPath.string() + " (" + ec.message() + ")";
return false;
}
module.print(os, nullptr);
os.flush();
return true;
}
bool buildCompileArgs(const std::string& filename, LanguageType language,
const AnalysisConfig& config, std::vector<std::string>& args,
std::string& workingDir, std::string& error)
{
const CompileCommand* command = nullptr;
if (config.compilationDatabase)
{
command = config.compilationDatabase->findCommandForFile(filename);
}
if (command)
{
args = command->arguments;
workingDir = command->directory;
if (config.compdbFast)
applyCompdbFastMode(args);
}
else
{
if (config.requireCompilationDatabase)
{
error = "No compile command found for: " + filename;
if (config.compilationDatabase &&
!config.compilationDatabase->sourcePath().empty())
{
error += " in " + config.compilationDatabase->sourcePath();
}
return false;
}
args.clear();
args.push_back("-emit-llvm");
args.push_back("-S");
args.push_back("-g");
if (language == LanguageType::CXX)
{
args.push_back("-x");
args.push_back("c++");
args.push_back("-std=gnu++20");
}
}
for (const auto& extraArg : config.extraCompileArgs)
{
args.push_back(extraArg);
}
appendIfMissing(args, "-emit-llvm");
appendIfMissing(args, "-S");
if (!hasDebugFlag(args))
args.push_back("-g");
appendIfMissing(args, "-fno-discard-value-names");
const bool useAbsolutePath = (command != nullptr);
args.push_back(useAbsolutePath ? makeAbsolutePath(filename) : filename);
return true;
}
class ScopedCurrentPath
{
public:
explicit ScopedCurrentPath(const std::string& path, std::string& error)
{
if (path.empty())
return;
std::error_code ec;
previousPath_ = std::filesystem::current_path(ec);
if (ec)
{
error = "Failed to read current working directory";
return;
}
std::filesystem::current_path(path, ec);
if (ec)
{
error = "Failed to change working directory to: " + path;
return;
}
active_ = true;
}
~ScopedCurrentPath()
{
if (!active_)
return;
std::error_code ec;
std::filesystem::current_path(previousPath_, ec);
}
private:
std::filesystem::path previousPath_;
std::uint64_t active_ : 1 = false;
std::uint64_t reservedFlags_ : 63 = 0;
};
class ScopedFileCleanup
{
public:
explicit ScopedFileCleanup(const std::filesystem::path& path) : path_(path) {}
~ScopedFileCleanup()
{
std::error_code ec;
if (!path_.empty())
std::filesystem::remove(path_, ec);
}
private:
std::filesystem::path path_;
};
} // namespace
static std::string extractExtension(const std::string& path)
{
const auto pos = path.find_last_of('.');
if (pos == std::string::npos || pos + 1 >= path.size())
return {};
return path.substr(pos + 1);
}
static std::string lowercaseCopy(std::string value)
{
std::transform(value.begin(), value.end(), value.begin(),
[](unsigned char c) { return std::tolower(c); });
return value;
}
LanguageType detectFromExtension(const std::string& path)
{
const std::string rawExt = extractExtension(path);
if (rawExt.empty())
return LanguageType::Unknown;
// Preserve historic ".C" semantics as C++ while still matching lower-case variants.
if (rawExt == "C")
return LanguageType::CXX;
const std::string ext = lowercaseCopy(rawExt);
if (ext == "ll" || ext == "bc")
return LanguageType::LLVM_IR;
if (ext == "c")
return LanguageType::C;
if (ext == "cpp" || ext == "cc" || ext == "cxx" || ext == "c++" || ext == "cp")
return LanguageType::CXX;
return LanguageType::Unknown;
}
LanguageType detectLanguageFromFile(const std::string& path, llvm::LLVMContext&)
{
return detectFromExtension(path);
}
ModuleLoadResult loadModuleForAnalysis(const std::string& filename,
const AnalysisConfig& config, llvm::LLVMContext& ctx,
llvm::SMDiagnostic& err)
{
using ctrace::stack::analyzer::ScopedHotspot;
ModuleLoadResult result;
const ScopedHotspot totalHotspot(config.timing, "input.load_module.total");
std::error_code cwdErr;
std::filesystem::path baseDir = std::filesystem::current_path(cwdErr);
using Clock = std::chrono::steady_clock;
auto compileStart = Clock::now();
{
const ScopedHotspot hotspot(config.timing, "input.detect_language");
result.language = detectLanguageFromFile(filename, ctx);
}
if (result.language == LanguageType::Unknown)
{
result.error = "Unsupported input file type: " + filename + "\n";
return result;
}
if (result.language != LanguageType::LLVM_IR)
{
std::vector<std::string> args;
std::string workingDir;
std::string compileError;
std::string compileDiagnosticsText;
bool compileArgsReady = false;
{
const ScopedHotspot hotspot(config.timing, "input.build_compile_args");
compileArgsReady = buildCompileArgs(filename, result.language, config, args,
workingDir, compileError);
}
if (!compileArgsReady)
{
result.error = compileError + "\n";
return result;
}
if (config.timing)
coretrace::log(coretrace::Level::Info, "Compiling {}...\n", filename);
const bool preferBitcodeCompile = (config.compileIRFormat == CompileIRFormat::BC);
const CompileIRCachePaths cachePaths =
buildCompileIRCachePaths(config, filename, result.language, args, workingDir);
std::error_code tempDirErr;
std::filesystem::path tempDir = std::filesystem::temp_directory_path(tempDirErr);
if (tempDirErr)
tempDir = std::filesystem::path(".");
const std::string tempBitcodeName =
"coretrace-compile-ir-" +
md5Hex(filename + "|" + std::to_string(compileStart.time_since_epoch().count())) +
".bc";
const std::filesystem::path tempBitcodePath = tempDir / tempBitcodeName;
const ScopedFileCleanup tempBitcodeCleanup(tempBitcodePath);
const std::vector<std::string> bitcodeArgs =
buildBitcodeCompileArgs(args, tempBitcodePath);
auto compileWithOptionalWorkingDir =
[&](const std::vector<std::string>& compileArgs, compilerlib::OutputMode outputMode,
bool useWorkingDir) -> std::optional<compilerlib::CompileResult>
{
if (!useWorkingDir)
{
const ScopedHotspot hotspot(config.timing, "input.compiler.invoke");
return compilerlib::compile(compileArgs, outputMode);
}
std::string cwdError;
ScopedCurrentPath cwdGuard(workingDir, cwdError);
if (!cwdError.empty())
{
result.error = cwdError + "\n";
return std::nullopt;
}
const ScopedHotspot hotspot(config.timing, "input.compiler.invoke.cwd");
return compilerlib::compile(compileArgs, outputMode);
};
auto compileWithConfiguredWorkingDir =
[&](const std::vector<std::string>& compileArgs, compilerlib::OutputMode outputMode,
bool& retriedWithWorkingDir) -> std::optional<compilerlib::CompileResult>
{
retriedWithWorkingDir = false;
std::optional<compilerlib::CompileResult> res;
const bool hasWorkingDir = !workingDir.empty();
if (config.jobs > 1 && hasWorkingDir)
{
// Optimistic fast path for multi-job runs: most compdb commands use absolute
// paths.
res = compileWithOptionalWorkingDir(compileArgs, outputMode, false);
if (!res || !res->success)
{
// Fallback keeps correctness for relative include paths and avoids process
// cwd races.
std::lock_guard<std::mutex> lock(gCompileWorkingDirMutex);
res = compileWithOptionalWorkingDir(compileArgs, outputMode, true);
retriedWithWorkingDir = true;
}
}
else
{
res = compileWithOptionalWorkingDir(compileArgs, outputMode, hasWorkingDir);
}
return res;
};
if (cachePaths.enabled)
{
auto cached = [&]() -> std::optional<CompileIRCachePayload>
{
const ScopedHotspot hotspot(config.timing, "input.cache.lookup.compile");
return loadCompileIRCachePayload(cachePaths);
}();
if (cached)
{
if (config.timing)
coretrace::log(coretrace::Level::Info, "Compilation cache hit for {}\n",
filename);
compileDiagnosticsText = cached->diagnostics;
if (!compileDiagnosticsText.empty() && !config.quiet)
logText(coretrace::Level::Warn, compileDiagnosticsText);
const auto parseStart = Clock::now();
auto tryParseCachedBitcode = [&]()
{
if (result.module || cached->llvmBitcode.empty())
return;
auto bcBuffer = llvm::MemoryBuffer::getMemBufferCopy(
llvm::StringRef(cached->llvmBitcode.data(), cached->llvmBitcode.size()),
"cached_ir_bc");
auto bitcodeModule = [&]()
{
const ScopedHotspot hotspot(config.timing,
"input.cache.parse_bitcode_payload");
return llvm::parseBitcodeFile(bcBuffer->getMemBufferRef(), ctx);
}();
if (bitcodeModule)
{
result.module = std::move(*bitcodeModule);
if (config.timing)
{
const auto parseEnd = Clock::now();
const auto ms =
std::chrono::duration_cast<std::chrono::milliseconds>(
parseEnd - parseStart)
.count();
coretrace::log(coretrace::Level::Info,
"Bitcode parse done in {} ms\n", ms);
}
return;
}
if (config.timing)
{
std::string bitcodeError = llvm::toString(bitcodeModule.takeError());
coretrace::log(coretrace::Level::Warn,
"Compilation cache bitcode invalid for {}; "
"falling back to textual IR ({})\n",
filename, bitcodeError);
}
};
auto tryParseCachedTextIR = [&]()
{
if (result.module || cached->llvmIR.empty())
return;
auto buffer = llvm::MemoryBuffer::getMemBuffer(cached->llvmIR, "cached_ir");
llvm::SMDiagnostic diag;
{
const ScopedHotspot hotspot(config.timing,
"input.cache.parse_text_payload");
result.module = llvm::parseIR(buffer->getMemBufferRef(), diag, ctx);
}
if (config.timing)
{
const auto parseEnd = Clock::now();
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
parseEnd - parseStart)
.count();
coretrace::log(coretrace::Level::Info, "IR parse done in {} ms\n", ms);
}
};
if (preferBitcodeCompile)
{
tryParseCachedBitcode();
tryParseCachedTextIR();
}
else
{