xref: /aosp_15_r20/external/stg/input.cc (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1*9e3b08aeSAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2*9e3b08aeSAndroid Build Coastguard Worker // -*- mode: C++ -*-
3*9e3b08aeSAndroid Build Coastguard Worker //
4*9e3b08aeSAndroid Build Coastguard Worker // Copyright 2022-2023 Google LLC
5*9e3b08aeSAndroid Build Coastguard Worker //
6*9e3b08aeSAndroid Build Coastguard Worker // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7*9e3b08aeSAndroid Build Coastguard Worker // "License"); you may not use this file except in compliance with the
8*9e3b08aeSAndroid Build Coastguard Worker // License.  You may obtain a copy of the License at
9*9e3b08aeSAndroid Build Coastguard Worker //
10*9e3b08aeSAndroid Build Coastguard Worker //     https://llvm.org/LICENSE.txt
11*9e3b08aeSAndroid Build Coastguard Worker //
12*9e3b08aeSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
13*9e3b08aeSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
14*9e3b08aeSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*9e3b08aeSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
16*9e3b08aeSAndroid Build Coastguard Worker // limitations under the License.
17*9e3b08aeSAndroid Build Coastguard Worker //
18*9e3b08aeSAndroid Build Coastguard Worker // Author: Giuliano Procida
19*9e3b08aeSAndroid Build Coastguard Worker 
20*9e3b08aeSAndroid Build Coastguard Worker #include "input.h"
21*9e3b08aeSAndroid Build Coastguard Worker 
22*9e3b08aeSAndroid Build Coastguard Worker #include <memory>
23*9e3b08aeSAndroid Build Coastguard Worker #include <sstream>
24*9e3b08aeSAndroid Build Coastguard Worker 
25*9e3b08aeSAndroid Build Coastguard Worker #include "abigail_reader.h"
26*9e3b08aeSAndroid Build Coastguard Worker #include "btf_reader.h"
27*9e3b08aeSAndroid Build Coastguard Worker #include "elf_dwarf_handle.h"
28*9e3b08aeSAndroid Build Coastguard Worker #include "elf_reader.h"
29*9e3b08aeSAndroid Build Coastguard Worker #include "error.h"
30*9e3b08aeSAndroid Build Coastguard Worker #include "filter.h"
31*9e3b08aeSAndroid Build Coastguard Worker #include "graph.h"
32*9e3b08aeSAndroid Build Coastguard Worker #include "proto_reader.h"
33*9e3b08aeSAndroid Build Coastguard Worker #include "reader_options.h"
34*9e3b08aeSAndroid Build Coastguard Worker #include "runtime.h"
35*9e3b08aeSAndroid Build Coastguard Worker 
36*9e3b08aeSAndroid Build Coastguard Worker namespace stg {
37*9e3b08aeSAndroid Build Coastguard Worker 
38*9e3b08aeSAndroid Build Coastguard Worker namespace {
39*9e3b08aeSAndroid Build Coastguard Worker 
ReadInternal(Runtime & runtime,Graph & graph,InputFormat format,const char * input,ReadOptions options,const std::unique_ptr<Filter> & file_filter)40*9e3b08aeSAndroid Build Coastguard Worker Id ReadInternal(Runtime& runtime, Graph& graph, InputFormat format,
41*9e3b08aeSAndroid Build Coastguard Worker                 const char* input, ReadOptions options,
42*9e3b08aeSAndroid Build Coastguard Worker                 const std::unique_ptr<Filter>& file_filter) {
43*9e3b08aeSAndroid Build Coastguard Worker   switch (format) {
44*9e3b08aeSAndroid Build Coastguard Worker     case InputFormat::ABI: {
45*9e3b08aeSAndroid Build Coastguard Worker       const Time read(runtime, "read ABI");
46*9e3b08aeSAndroid Build Coastguard Worker       return abixml::Read(runtime, graph, input);
47*9e3b08aeSAndroid Build Coastguard Worker     }
48*9e3b08aeSAndroid Build Coastguard Worker     case InputFormat::BTF: {
49*9e3b08aeSAndroid Build Coastguard Worker       const Time read(runtime, "read BTF");
50*9e3b08aeSAndroid Build Coastguard Worker       return btf::ReadFile(graph, input, options);
51*9e3b08aeSAndroid Build Coastguard Worker     }
52*9e3b08aeSAndroid Build Coastguard Worker     case InputFormat::ELF: {
53*9e3b08aeSAndroid Build Coastguard Worker       const Time read(runtime, "read ELF");
54*9e3b08aeSAndroid Build Coastguard Worker       ElfDwarfHandle elf_dwarf_handle(input);
55*9e3b08aeSAndroid Build Coastguard Worker       return elf::Read(runtime, graph, elf_dwarf_handle, options, file_filter);
56*9e3b08aeSAndroid Build Coastguard Worker     }
57*9e3b08aeSAndroid Build Coastguard Worker     case InputFormat::STG: {
58*9e3b08aeSAndroid Build Coastguard Worker       const Time read(runtime, "read STG");
59*9e3b08aeSAndroid Build Coastguard Worker       return proto::Read(runtime, graph, input);
60*9e3b08aeSAndroid Build Coastguard Worker     }
61*9e3b08aeSAndroid Build Coastguard Worker   }
62*9e3b08aeSAndroid Build Coastguard Worker }
63*9e3b08aeSAndroid Build Coastguard Worker 
64*9e3b08aeSAndroid Build Coastguard Worker }  // namespace
65*9e3b08aeSAndroid Build Coastguard Worker 
Read(Runtime & runtime,Graph & graph,InputFormat format,const char * input,ReadOptions options,const std::unique_ptr<Filter> & file_filter)66*9e3b08aeSAndroid Build Coastguard Worker Id Read(Runtime& runtime, Graph& graph, InputFormat format, const char* input,
67*9e3b08aeSAndroid Build Coastguard Worker         ReadOptions options, const std::unique_ptr<Filter>& file_filter) {
68*9e3b08aeSAndroid Build Coastguard Worker   try {
69*9e3b08aeSAndroid Build Coastguard Worker     return ReadInternal(runtime, graph, format, input, options, file_filter);
70*9e3b08aeSAndroid Build Coastguard Worker   } catch (Exception& e) {
71*9e3b08aeSAndroid Build Coastguard Worker     std::ostringstream os;
72*9e3b08aeSAndroid Build Coastguard Worker     os << "processing file '" << input << '\'';
73*9e3b08aeSAndroid Build Coastguard Worker     e.Add(os.str());
74*9e3b08aeSAndroid Build Coastguard Worker     throw;
75*9e3b08aeSAndroid Build Coastguard Worker   }
76*9e3b08aeSAndroid Build Coastguard Worker }
77*9e3b08aeSAndroid Build Coastguard Worker 
78*9e3b08aeSAndroid Build Coastguard Worker }  // namespace stg
79