|
| 1 | +#include "llvm/Transforms/IPO/MSVCMacroRebuilding.h" |
| 2 | +#include "llvm/ADT/StringRef.h" |
| 3 | +#include "llvm/Analysis/ModuleSummaryAnalysis.h" |
| 4 | +#include "llvm/Demangle/Demangle.h" |
| 5 | +#include "llvm/IR/Function.h" |
| 6 | +#include "llvm/IR/IRBuilder.h" |
| 7 | +#include "llvm/IR/InlineAsm.h" |
| 8 | +#include "llvm/IR/Module.h" |
| 9 | +#include "llvm/IR/PrintPasses.h" |
| 10 | +#include "llvm/IRPrinter/IRAutoGeneratorPass.h" |
| 11 | +#include "llvm/Pass.h" |
| 12 | +#include "llvm/Support/Debug.h" |
| 13 | +#include "llvm/Support/FileSystem.h" |
| 14 | +#include "llvm/Support/Path.h" |
| 15 | +#include "llvm/Support/ToolOutputFile.h" |
| 16 | +#include "llvm/Support/raw_ostream.h" |
| 17 | +#include "llvm/Transforms/Utils/ModuleUtils.h" |
| 18 | +#include <fstream> |
| 19 | +#include <iostream> |
| 20 | +#include <regex> |
| 21 | + |
| 22 | +using namespace llvm; |
| 23 | + |
| 24 | +// Replace '__FUNCTION__' |
| 25 | +bool MSVCMacroRebuildingPass::replace__FUNCTION__(GlobalVariable &GV, |
| 26 | + ConstantDataArray *CDA, |
| 27 | + StringRef FunctionName) { |
| 28 | + // Construct the regular expression used to match the |
| 29 | + // macro marker and the file name with escaped backslashes and line number. |
| 30 | + static std::string RegexStr = |
| 31 | + MSVCMacroRebuildingPass::get__FUNCTION__MarkerName().str() + |
| 32 | + std::regex_replace(GV.getParent()->getSourceFileName(), |
| 33 | + std::regex("\\\\"), "\\\\") + |
| 34 | + "\\(Line:\\d+\\)"; |
| 35 | + |
| 36 | + // Get the original string value of the constant data array. |
| 37 | + std::string OriginalStr = CDA->getAsCString().str(); |
| 38 | + // Replace the macro marker and file name in the original string with |
| 39 | + // the name of the function containing the instruction. |
| 40 | + std::string ReplacedStr = |
| 41 | + std::regex_replace(OriginalStr, std::regex(RegexStr), FunctionName.str()); |
| 42 | + |
| 43 | + // If the replaced string is the same as the original string, skip the |
| 44 | + // variable. |
| 45 | + if (ReplacedStr == OriginalStr) |
| 46 | + return false; |
| 47 | + |
| 48 | + // Demangle the replaced string and extract the function name. |
| 49 | + std::string DemangledReplacedStr = demangle(ReplacedStr); |
| 50 | + size_t StartIndex = DemangledReplacedStr.find("("); |
| 51 | + if (StartIndex != std::string::npos) { |
| 52 | + size_t EndIndex = DemangledReplacedStr.rfind(" ", StartIndex); |
| 53 | + if (EndIndex != std::string::npos) { |
| 54 | + DemangledReplacedStr = |
| 55 | + DemangledReplacedStr.substr(EndIndex + 1, StartIndex - EndIndex - 1); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Create a new constant data array containing the demangled function |
| 60 | + // name and set it as the initializer of the global variable. |
| 61 | + Constant *NewStr = ConstantDataArray::getString(GV.getParent()->getContext(), |
| 62 | + DemangledReplacedStr); |
| 63 | + GV.setInitializer(NewStr); |
| 64 | + |
| 65 | + return true; |
| 66 | +} |
| 67 | + |
| 68 | +// Implementation of the run() function for the MSVCMacroRebuildingPass |
| 69 | +// class |
| 70 | +PreservedAnalyses MSVCMacroRebuildingPass::run(Module &M, |
| 71 | + ModuleAnalysisManager &AM) { |
| 72 | + bool Changed = false; |
| 73 | + |
| 74 | + // Iterate over all global variables in the input module. |
| 75 | + for (GlobalVariable &GV : M.globals()) { |
| 76 | + // Skip the variable if it does not have an initializer. |
| 77 | + if (!GV.hasInitializer()) |
| 78 | + continue; |
| 79 | + |
| 80 | + // Check if the initializer is a constant data array of strings. |
| 81 | + if (ConstantDataArray *CDA = |
| 82 | + dyn_cast<ConstantDataArray>(GV.getInitializer())) { |
| 83 | + if (!CDA->isString()) |
| 84 | + continue; |
| 85 | + |
| 86 | + // Iterate over all users of the global variable and check if it is an |
| 87 | + // instruction in a function. |
| 88 | + for (auto UserIt = GV.users().begin(); UserIt != GV.users().end(); |
| 89 | + ++UserIt) { |
| 90 | + if (Instruction *Inst = dyn_cast<Instruction>(*UserIt)) { |
| 91 | + if (!Inst->getParent()) |
| 92 | + continue; |
| 93 | + |
| 94 | + Function *F = Inst->getParent()->getParent(); |
| 95 | + if (!F) |
| 96 | + continue; |
| 97 | + |
| 98 | + // Replace '__FUNCTION__' |
| 99 | + Changed |= replace__FUNCTION__(GV, CDA, F->getName()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * This statement returns a PreservedAnalyses object based on whether there |
| 107 | + * were any changes during the pass execution. If `Changed` is true, |
| 108 | + * indicating that changes occurred, it returns |
| 109 | + * `llvm::PreservedAnalyses::none()`. This means that some analyses may not be |
| 110 | + * preserved and should be re-run as the pass has potentially invalidated |
| 111 | + * their results. If `Changed` is false, indicating that no changes occurred, |
| 112 | + * it returns `llvm::PreservedAnalyses::all()`. This means that all analyses |
| 113 | + * are preserved and their results are still valid after the pass execution. |
| 114 | + */ |
| 115 | + return (Changed ? llvm::PreservedAnalyses::none() |
| 116 | + : llvm::PreservedAnalyses::all()); |
| 117 | +} |
0 commit comments