xref: /aosp_15_r20/build/make/tools/ide_query/cc_analyzer/main.cc (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // Driver for c++ extractor. Operates in two modes:
18 // - DEPS, scans build graph for active files and reports targets that need to
19 // be build for analyzing that file.
20 // - INPUTS, scans the source code for active files and returns all the sources
21 // required for analyzing that file.
22 //
23 // Uses stdin/stdout to take in requests and provide responses.
24 #include <unistd.h>
25 
26 #include <memory>
27 #include <utility>
28 
29 #include "analyzer.h"
30 #include "google/protobuf/message.h"
31 #include "cc_analyzer.pb.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/TargetSelect.h"
35 #include "llvm/Support/raw_ostream.h"
36 
37 namespace {
38 enum class OpMode {
39   DEPS = 0,
40   INPUTS = 1,
41 };
42 llvm::cl::opt<OpMode> mode{
43     "mode",
44     llvm::cl::values(clEnumValN(OpMode::DEPS, "deps",
45                                 "Figure out targets that need to be build"),
46                      clEnumValN(OpMode::INPUTS, "inputs",
47                                 "Figure out generated files used")),
48     llvm::cl::desc("Print the list of headers to insert and remove"),
49 };
50 
ReturnError(llvm::StringRef message)51 cc_analyzer::IdeAnalysis ReturnError(llvm::StringRef message) {
52   cc_analyzer::IdeAnalysis result;
53   result.mutable_status()->set_code(cc_analyzer::Status::FAILURE);
54   result.mutable_status()->set_message(message.str());
55   return result;
56 }
57 
58 }  // namespace
59 
main(int argc,char * argv[])60 int main(int argc, char* argv[]) {
61   llvm::InitializeAllTargetInfos();
62   llvm::cl::ParseCommandLineOptions(argc, argv);
63 
64   cc_analyzer::RepoState state;
65   if (!state.ParseFromFileDescriptor(STDIN_FILENO)) {
66     llvm::errs() << "Failed to parse input!\n";
67     return 1;
68   }
69 
70   std::unique_ptr<google::protobuf::Message> result;
71   switch (mode) {
72     case OpMode::DEPS: {
73       result = std::make_unique<cc_analyzer::DepsResponse>(
74           tools::ide_query::cc_analyzer::GetDeps(std::move(state)));
75       break;
76     }
77     case OpMode::INPUTS: {
78       result = std::make_unique<cc_analyzer::IdeAnalysis>(
79           tools::ide_query::cc_analyzer::GetBuildInputs(std::move(state)));
80       break;
81     }
82     default:
83       llvm::errs() << "Unknown operation mode!\n";
84       return 1;
85   }
86   if (!result->SerializeToFileDescriptor(STDOUT_FILENO)) {
87     llvm::errs() << "Failed to serialize result!\n";
88     return 1;
89   }
90 
91   return 0;
92 }
93