Debug type-decl serialization

* src/abg-writer.cc (do_indent): New function.
	(write_corpus&): Use do_indent.  Don't forget to close the
	abi-instr tag.
	(write_type_decl): Use do_indent.  Handle null decls pointers.
	Emit the id at the end of the element.
	(write_namespace_decl): Use do_indent.  Handle null decls
	pointers.
	* tests/test-utils.{h,cc} (get_src_dir,get_build_dir): Constify
	the returned reference to string.
	(is_dir, ensure_dir_path_created, ensure_parent_dir_created): New
	function definitions.
	* tests/test-read-write.cc (main): Augment the test to serialize
	the corpus too.
This commit is contained in:
Dodji Seketeli 2013-03-22 16:53:26 +01:00
parent fc27d10cee
commit e25159b4b9
4 changed files with 142 additions and 24 deletions

View File

@ -109,6 +109,16 @@ static bool write_namespace_decl(const shared_ptr<namespace_decl>,
write_context&,
unsigned);
void do_indent(ostream&, unsigned);
/// Emit #nb_whitespaces white spaces into the output stream #o.
void
do_indent(ostream& o, unsigned nb_whitespaces)
{
for (unsigned i = 0; i < nb_whitespaces; ++i)
o << ' ';
}
/// Serialize an abi corpus into an output stream.
///
/// \param corpus the corpus to serialize
@ -171,12 +181,11 @@ write_corpus(const abi_corpus& corpus, write_context& ctxt, unsigned indent)
ostream &o = ctxt.get_ostream();
const config &c = ctxt.get_config();
for (unsigned i = 0; i < indent; ++i)
o << ' ';
do_indent(o, indent);
o << "<abi-instr version='"
<< c.get_format_major_version_number()
<< "." << c.get_format_minor_version_number()
<< static_cast<int> (c.get_format_major_version_number())
<< "." << static_cast<int>(c.get_format_minor_version_number())
<< "'";
if (corpus.is_empty())
@ -184,6 +193,8 @@ write_corpus(const abi_corpus& corpus, write_context& ctxt, unsigned indent)
o << "/>";
return true;
}
else
o << ">";
for (abi_corpus::decls_type::const_iterator i = corpus.get_decls().begin();
i != corpus.get_decls().end();
@ -194,6 +205,10 @@ write_corpus(const abi_corpus& corpus, write_context& ctxt, unsigned indent)
indent + c.get_xml_element_indent());
}
o << "\n";
do_indent(o, indent);
o << "</abi-instr>\n";
return true;
}
@ -218,15 +233,14 @@ write_type_decl(const shared_ptr<type_decl> d,
write_context& ctxt,
unsigned indent)
{
if (!d)
return false;
ostream &o = ctxt.get_ostream();
for (unsigned i = 0; i < indent; ++i)
o << ' ';
do_indent(o, indent);
o << "<type-decl name='" << d->get_name() << "'"
<< "xml:id='"
<< ctxt.get_id_manager().get_id_with_prefix("type-decl-")
<< "'";
o << "<type-decl name='" << d->get_name() << "'";
size_t size_in_bits = d->get_size_in_bits();
if (size_in_bits)
@ -245,6 +259,11 @@ write_type_decl(const shared_ptr<type_decl> d,
<< " line='" << line << "'"
<< " column='" << column << "'";
}
o << " xml:id='"
<< ctxt.get_id_manager().get_id_with_prefix("type-decl-")
<< "'";
o<< "/>";
return true;
@ -270,11 +289,13 @@ write_namespace_decl(const shared_ptr<namespace_decl> decl,
write_context& ctxt,
unsigned indent)
{
if (!decl)
return false;
ostream &o = ctxt.get_ostream();
const config &c = ctxt.get_config();
for (unsigned i = 0; i < indent; ++i)
o << ' ';
do_indent(o, indent);
o << "<namespace-decl name='" << decl->get_name() << "'>";

View File

@ -1,22 +1,37 @@
// -*- Mode: C++ -*-
#include <iostream>
#include <string>
#include <fstream>
#include "test-utils.h"
#include "abg-reader.h"
#include "abg-writer.h"
using std::string;
using std::ofstream;
int
main()
{
string suffix("tests/data/test-read-write/input0.xml");
string path(abigail::tests::get_src_dir() + "/" + suffix);
unsigned result = 1;
string input_suffix("tests/data/test-read-write/input0.xml");
string path(abigail::tests::get_src_dir() + "/" + input_suffix);
abigail::abi_corpus corpus("test");
if (!abigail::reader::read_file(path.c_str(), corpus))
return 1;
return result;
//TODO: serialize the corpus into
//builddir/output/test-read-write/ouput0.xml, so that an other
//dedicated test diffs it.
string output_suffix("tests/output/test-read-write/input0.xml");
path = abigail::tests::get_build_dir() + "/" + output_suffix;
if (!abigail::tests::ensure_parent_dir_created(path))
return result;
return 0;
ofstream of(path.c_str(), std::ios_base::trunc);
if (!of.is_open())
return result;
bool is_ok = abigail::writer::write_to_ostream(corpus, of);
of.close();
return !is_ok;
}

View File

@ -1,5 +1,11 @@
// -*- Mode: C++ -*-
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libgen.h>
#include <cstring>
#include <cstdlib>
#include "test-utils.h"
using std::string;
@ -9,7 +15,10 @@ namespace abigail
namespace tests
{
std::string&
/// Returns the absolute path to the source directory.
///
/// \return the absolute path tho the source directory.
const std::string&
get_src_dir()
{
#ifndef ABIGAIL_SRC_DIR
@ -20,7 +29,10 @@ get_src_dir()
return s;
}
std::string&
/// Returns the absolute path to the build directory.
///
/// \return the absolute path the build directory.
const std::string&
get_build_dir()
{
#ifndef ABIGAIL_BUILD_DIR
@ -31,5 +43,72 @@ get_build_dir()
return s;
}
/// Tests whether #path is a directory.
///
/// \return true iff #path is a directory.
bool
is_dir(const string& path)
{
struct stat st;
memset(&st, 0, sizeof (st));
if (stat(path.c_str(), &st) != 0)
return false;
return !!S_ISDIR(st.st_mode);
}
/// Ensures #dir_path is a directory and is created. If #dir_path is
/// not created, this function creates it.
///
/// \return true if #dir_path is a directory that is already present,
/// of if the function has successfuly created it.
bool
ensure_dir_path_created(const string& dir_path)
{
struct stat st;
memset(&st, 0, sizeof (st));
int stat_result = 0;
stat_result = stat(dir_path.c_str(), &st);
if (stat_result == 0)
{
// A file or directory already exists with the same name.
if (!S_ISDIR (st.st_mode))
return false;
return true;
}
string cmd;
cmd = "mkdir -p " + dir_path;
if (system(cmd.c_str()))
return false;
return true;
}
/// Ensures that the parent directory of #path is created.
///
/// \return true if the parent directory of #path is already present,
/// or if this function has successfuly created it.
bool
ensure_parent_dir_created(const string& path)
{
bool is_ok = false;
if (path.empty())
return is_ok;
char * p = strdup(path.c_str());
char *parent = dirname(p);
is_ok = ensure_dir_path_created(parent);
free(p);
return is_ok;
}
}//end namespace tests
}//end namespace abigail

View File

@ -10,8 +10,11 @@ namespace abigail
namespace tests
{
std::string& get_src_dir();
std::string& get_build_dir();
const std::string& get_src_dir();
const std::string& get_build_dir();
bool is_dir(const std::string&);
bool ensure_dir_path_created(const std::string&);
bool ensure_parent_dir_created(const std::string&);
}//end namespace tests
}//end namespace abigail