Skip to content

Commit 08ad919

Browse files
author
Thomas Kiley
authored
Merge pull request #1913 from thk123/refactor/lazy-load-java-class-unit-tests
Refactor load java class to allow lazy loading of methods
2 parents 1abbaff + 3864e6c commit 08ad919

File tree

5 files changed

+121
-4
lines changed

5 files changed

+121
-4
lines changed

unit/testing-utils/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
SRC = \
22
c_to_expr.cpp \
3+
free_form_cmdline.cpp \
34
load_java_class.cpp \
45
require_expr.cpp \
56
require_goto_statements.cpp \
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited.
6+
7+
\*******************************************************************/
8+
9+
#include "free_form_cmdline.h"
10+
11+
/// Create a command line option that can be set
12+
/// \param flag_name: The name of the command line option to support
13+
void free_form_cmdlinet::create_flag(const std::string &flag_name)
14+
{
15+
optiont option;
16+
option.optstring = flag_name;
17+
options.push_back(option);
18+
}
19+
20+
/// Equivalent to specifying --flag for the command line
21+
/// \param flag: The name of the flag to specify
22+
void free_form_cmdlinet::add_flag(std::string flag)
23+
{
24+
create_flag(flag);
25+
set(flag);
26+
}
27+
28+
/// Equivalent to specifying --flag value
29+
/// \param flag: The name of the flag to specify
30+
/// \param value: The value to the set the command line option to
31+
void free_form_cmdlinet::add_option(std::string flag, std::string value)
32+
{
33+
create_flag(flag);
34+
set(flag, value);
35+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test utilities
4+
5+
Author: DiffBlue Limited.
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_TESTING_UTILS_FREE_FORM_CMDLINE_H
10+
#define CPROVER_TESTING_UTILS_FREE_FORM_CMDLINE_H
11+
12+
#include <util/cmdline.h>
13+
14+
/// An implementation of cmdlinet to be used in tests. It does not require
15+
/// specifying exactly what flags are supported and instead allows setting any
16+
/// flag
17+
class free_form_cmdlinet : public cmdlinet
18+
{
19+
public:
20+
void add_flag(std::string flag);
21+
void add_option(std::string flag, std::string value);
22+
23+
private:
24+
void create_flag(const std::string &flag_name);
25+
};
26+
27+
#endif // CPROVER_TESTING_UTILS_FREE_FORM_CMDLINE_H

unit/testing-utils/load_java_class.cpp

+45-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,43 @@
77
\*******************************************************************/
88

99
#include "load_java_class.h"
10+
#include "free_form_cmdline.h"
1011
#include <testing-utils/catch.hpp>
1112
#include <iostream>
1213

1314
#include <util/config.h>
14-
#include <util/options.h>
1515
#include <util/suffix.h>
1616

1717
#include <goto-programs/lazy_goto_model.h>
1818

1919
#include <java_bytecode/java_bytecode_language.h>
2020

21+
/// Go through the process of loading, type-checking and finalising loading a
22+
/// specific class file to build the symbol table. The functions are converted
23+
/// using ci_lazy_methods (equivalent to passing --lazy-methods to JBMC)
24+
/// \param java_class_name: The name of the class file to load. It should not
25+
/// include the .class extension.
26+
/// \param class_path: The path to load the class from. Should be relative to
27+
/// the unit directory.
28+
/// \param main: The name of the main function or "" to use the default
29+
/// behaviour to find a main function.
30+
/// \return The symbol table that is generated by parsing this file.
31+
symbol_tablet load_java_class_lazy(
32+
const std::string &java_class_name,
33+
const std::string &class_path,
34+
const std::string &main)
35+
{
36+
free_form_cmdlinet lazy_command_line;
37+
lazy_command_line.add_flag("lazy-methods");
38+
39+
return load_java_class(
40+
java_class_name,
41+
class_path,
42+
main,
43+
new_java_bytecode_language(),
44+
lazy_command_line);
45+
}
46+
2147
/// Go through the process of loading, type-checking and finalising loading a
2248
/// specific class file to build the symbol table.
2349
/// \param java_class_name: The name of the class file to load. It should not
@@ -51,7 +77,8 @@ symbol_tablet load_java_class(
5177
const std::string &java_class_name,
5278
const std::string &class_path,
5379
const std::string &main,
54-
std::unique_ptr<languaget> &&java_lang)
80+
std::unique_ptr<languaget> &&java_lang,
81+
const cmdlinet &command_line)
5582
{
5683
// We expect the name of the class without the .class suffix to allow us to
5784
// check it
@@ -66,8 +93,6 @@ symbol_tablet load_java_class(
6693
message_handler);
6794

6895
// Configure the path loading
69-
cmdlinet command_line;
70-
command_line.set("java-cp-include-files", class_path);
7196
config.java.classpath.clear();
7297
config.java.classpath.push_back(class_path);
7398
config.main = main;
@@ -109,3 +134,19 @@ symbol_tablet load_java_class(
109134
REQUIRE_FALSE(class_type.get_bool(ID_incomplete_class));
110135
return std::move(maybe_goto_model->symbol_table);
111136
}
137+
138+
symbol_tablet load_java_class(
139+
const std::string &java_class_name,
140+
const std::string &class_path,
141+
const std::string &main,
142+
std::unique_ptr<languaget> &&java_lang)
143+
{
144+
cmdlinet command_line;
145+
// TODO(tkiley): This doesn't do anything as "java-cp-include-files" is an
146+
// TODO(tkiley): unknown argument. This could be changed by using the
147+
// TODO(tkiley): free_form_cmdlinet however this causes some tests to fail.
148+
// TODO(tkiley): TG-2708 to investigate and fix
149+
command_line.set("java-cp-include-files", class_path);
150+
return load_java_class(
151+
java_class_name, class_path, main, std::move(java_lang), command_line);
152+
}

unit/testing-utils/load_java_class.h

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <goto-programs/goto_model.h>
1919

2020
#include <langapi/language.h>
21+
#include <util/cmdline.h>
2122

2223
symbol_tablet load_java_class(
2324
const std::string &java_class_name,
@@ -30,4 +31,16 @@ symbol_tablet load_java_class(
3031
const std::string &main,
3132
std::unique_ptr<languaget> &&java_lang);
3233

34+
symbol_tablet load_java_class(
35+
const std::string &java_class_name,
36+
const std::string &class_path,
37+
const std::string &main,
38+
std::unique_ptr<languaget> &&java_lang,
39+
const cmdlinet &command_line);
40+
41+
symbol_tablet load_java_class_lazy(
42+
const std::string &java_class_name,
43+
const std::string &class_path,
44+
const std::string &main);
45+
3346
#endif // CPROVER_TESTING_UTILS_LOAD_JAVA_CLASS_H

0 commit comments

Comments
 (0)