Skip to content

Commit a590313

Browse files
frontend: add module to expose core operations (#1891)
* frontend: add module to expose core operations Signed-off-by: David Korczynski <[email protected]> * nit Signed-off-by: David Korczynski <[email protected]> * show more extensions Signed-off-by: David Korczynski <[email protected]> --------- Signed-off-by: David Korczynski <[email protected]>
1 parent 78c3c02 commit a590313

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2024 Fuzz Introspector Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""APIs that make it easy to access frontend analysis."""
15+
16+
import logging
17+
18+
from fuzz_introspector.frontends import frontend_c
19+
from fuzz_introspector.frontends import frontend_cpp
20+
21+
logger = logging.getLogger(name=__name__)
22+
23+
24+
def analyse_source_file(code: bytes, language: str):
25+
"""Runs frontend analysis on a code snippet.
26+
27+
The code snippet should correspond to what you'd normally find in
28+
a source file, e.g. a number of functions, include statements and so
29+
on.
30+
31+
Returns a frontend Source code module if successful and None otherwise."""
32+
33+
if language == 'c':
34+
return frontend_c.analyse_source_code(code)
35+
elif language == 'cpp':
36+
return frontend_cpp.analyse_source_code(code)
37+
elif language == 'go':
38+
logger.info('Go is not implement yet.')
39+
else:
40+
logger.info('Language %s not supported', language)
41+
return None

src/fuzz_introspector/frontends/frontend_c.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ def find_source_with_func_def(self, target_function_name):
184184

185185
return None
186186

187+
def get_function(self, target_function_name):
188+
"""Gets the first instance of a given function."""
189+
190+
for source_code in self.source_code_files:
191+
func = source_code.get_function_node(target_function_name)
192+
if func is not None:
193+
return func
194+
return None
195+
187196

188197
class FunctionDefinition():
189198
"""Wrapper for a function definition"""

src/fuzz_introspector/frontends/frontend_cpp.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,12 @@ def load_treesitter_trees(source_files, log_harnesses=True):
377377
logger.info('harness: %s', code_file)
378378
results.append(source_cls)
379379
return results
380+
381+
382+
def analyse_source_code(source_content: str) -> Project:
383+
"""Returns a Project based on a single source string."""
384+
source_code = SourceCodeFile(source_file='in-memory string',
385+
language='cpp',
386+
source_content=source_content.encode())
387+
project = Project([source_code])
388+
return project

0 commit comments

Comments
 (0)