Static Value-Flow Analysis
llvm2svf.cpp
Go to the documentation of this file.
1 //===- llvm2svf.cpp -- LLVM IR to SVF IR conversion -------------------------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-2023> <Yulei Sui>
6 //
7 
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
17 
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 //===-----------------------------------------------------------------------===//
22 
23 /*
24  * llvm2svf.cpp
25  *
26  * Created on: 21 Apr 2023
27  * Authors: Xudong Wang
28  */
29 
30 #include "SVF-LLVM/SVFIRBuilder.h"
31 #include "Util/CommandLine.h"
32 #include "Util/Options.h"
33 #include "SVFIR/SVFFileSystem.h"
34 
35 
36 using namespace std;
37 using namespace SVF;
38 
39 #include <iostream>
40 #include <string>
41 
43 {
44  size_t pos = path.rfind('.');
45  if (pos == std::string::npos ||
46  (path.substr(pos) != ".bc" && path.substr(pos) != ".ll"))
47  {
48  SVFUtil::errs() << "Error: expect file with extension .bc or .ll\n";
49  exit(EXIT_FAILURE);
50  }
51  return path.substr(0, pos) + ".svf.json";
52 }
53 
54 int main(int argc, char** argv)
55 {
56  auto moduleNameVec = OptionBase::parseOptions(
57  argc, argv, "llvm2svf", "[options] <input-bitcode...>");
58 
59  if (Options::WriteAnder() == "ir_annotator")
60  {
61  LLVMModuleSet::preProcessBCs(moduleNameVec);
62  }
63 
64  SVFModule* svfModule = LLVMModuleSet::buildSVFModule(moduleNameVec);
65  const std::string jsonPath = replaceExtension(moduleNameVec.front());
66  // PAG is borrowed from a unique_ptr, so we don't need to delete it.
67  const SVFIR* pag = SVFIRBuilder(svfModule).build();
68 
69  SVFIRWriter::writeJsonToPath(pag, jsonPath);
70  SVFUtil::outs() << "SVF IR is written to '" << jsonPath << "'\n";
71  LLVMModuleSet::releaseLLVMModuleSet();
72  return 0;
73 }
const char *const string
Definition: cJSON.h:172
static std::vector< std::string > parseOptions(int argc, char *argv[], std::string description, std::string callFormat)
Definition: CommandLine.h:75
virtual SVFIR * build()
Start building SVFIR here.
int main(int argc, char **argv)
Definition: llvm2svf.cpp:54
std::string replaceExtension(const std::string &path)
Definition: llvm2svf.cpp:42
std::ostream & errs()
Overwrite llvm::errs()
Definition: SVFUtil.h:56
std::ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:50
for isBitcode
Definition: BasicTypes.h:68