Skip to content

Commit

Permalink
Python Metadata Validation (#3502)
Browse files Browse the repository at this point in the history
  • Loading branch information
InsertCreativityHere authored Feb 10, 2025
1 parent ed2bb01 commit c7a5a9b
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 279 deletions.
7 changes: 7 additions & 0 deletions cpp/src/Slice/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ namespace Slice
// ----------------------------------------------------------------------
// Metadata
// ----------------------------------------------------------------------

Slice::Metadata::Metadata(string rawMetadata, string file, int line)
{
_file = std::move(file);
Expand Down Expand Up @@ -874,6 +875,12 @@ Slice::Contained::container() const
return _container;
}

bool
Slice::Contained::isTopLevel() const
{
return dynamic_pointer_cast<Unit>(container()) != nullptr;
}

string
Slice::Contained::name() const
{
Expand Down
1 change: 1 addition & 0 deletions cpp/src/Slice/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ namespace Slice
public:
virtual void destroy() {}
[[nodiscard]] ContainerPtr container() const;
[[nodiscard]] bool isTopLevel() const;

/// Returns the Slice identifier of this element.
[[nodiscard]] std::string name() const;
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/ice2slice/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ namespace
}
}

ContainerPtr c = p->container();
p = dynamic_pointer_cast<Contained>(c); // This cast fails for Unit.
if (!p)
if (p->isTopLevel())
{
break;
}
p = dynamic_pointer_cast<Contained>(p->container());
assert(p);
}

assert(m);
Expand Down
8 changes: 2 additions & 6 deletions cpp/src/slice2cpp/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3254,11 +3254,9 @@ Slice::Gen::StreamVisitor::visitModuleStart(const ModulePtr& m)
return false;
}

if (dynamic_pointer_cast<Unit>(m->container()))
if (m->isTopLevel())
{
//
// Only emit this for the top-level module.
//
H << sp;
H << nl << "/// \\cond STREAM";
H << nl << "namespace Ice" << nl << '{';
Expand All @@ -3270,11 +3268,9 @@ Slice::Gen::StreamVisitor::visitModuleStart(const ModulePtr& m)
void
Slice::Gen::StreamVisitor::visitModuleEnd(const ModulePtr& m)
{
if (dynamic_pointer_cast<Unit>(m->container()))
if (m->isTopLevel())
{
//
// Only emit this for the top-level module.
//
H.dec();
H << nl << '}';
H << nl << "/// \\endcond";
Expand Down
8 changes: 4 additions & 4 deletions cpp/src/slice2cs/CsUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ Slice::CsGenerator::getNamespacePrefix(const ContainedPtr& cont)
m = dynamic_pointer_cast<Module>(p);
}

ContainerPtr c = p->container();
p = dynamic_pointer_cast<Contained>(c); // This cast fails for Unit.
if (!p)
if (p->isTopLevel())
{
break;
}
p = dynamic_pointer_cast<Contained>(p->container());
assert(p);
}

assert(m);
Expand Down Expand Up @@ -1849,7 +1849,7 @@ Slice::CsGenerator::validateMetadata(const UnitPtr& u)
{
// 'cs:namespace' can only be applied to top-level modules
// Top-level modules are contained by the 'Unit'. Non-top-level modules are contained in 'Module's.
if (auto mod = dynamic_pointer_cast<Module>(p); mod && !dynamic_pointer_cast<Unit>(mod->container()))
if (auto mod = dynamic_pointer_cast<Module>(p); mod && !mod->isTopLevel())
{
return "the 'cs:namespace' metadata can only be applied to top-level modules";
}
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/slice2cs/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ Slice::CsVisitor::writeParameterDocComments(const DocComment& comment, const Par
void
Slice::CsVisitor::moduleStart(const ModulePtr& p)
{
if (!dynamic_pointer_cast<Contained>(p->container()))
if (p->isTopLevel())
{
string ns = getNamespacePrefix(p);
if (!ns.empty())
Expand All @@ -768,7 +768,7 @@ Slice::CsVisitor::moduleStart(const ModulePtr& p)
void
Slice::CsVisitor::moduleEnd(const ModulePtr& p)
{
if (!dynamic_pointer_cast<Contained>(p->container()))
if (p->isTopLevel())
{
if (!getNamespacePrefix(p).empty())
{
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/slice2java/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2241,7 +2241,7 @@ bool
Slice::Gen::TypesVisitor::visitModuleStart(const ModulePtr& p)
{
string prefix = getPackagePrefix(p);
if (!prefix.empty() && dynamic_pointer_cast<Unit>(p->container())) // generate Marker class for top-level modules
if (!prefix.empty() && p->isTopLevel()) // generate Marker class for top-level modules
{
string markerClass = prefix + "." + fixKwd(p->name()) + "._Marker";
open(markerClass, p->file());
Expand Down
8 changes: 4 additions & 4 deletions cpp/src/slice2java/JavaUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,12 @@ Slice::JavaGenerator::getPackagePrefix(const ContainedPtr& cont)
m = dynamic_pointer_cast<Module>(p);
}

ContainerPtr c = p->container();
p = dynamic_pointer_cast<Contained>(c); // This cast fails for Unit.
if (!p)
if (p->isTopLevel())
{
break;
}
p = dynamic_pointer_cast<Contained>(p->container());
assert(p);
}

assert(m);
Expand Down Expand Up @@ -1839,7 +1839,7 @@ Slice::JavaGenerator::validateMetadata(const UnitPtr& u)
{
// If 'java:package' is applied to a module, it must be a top-level module.
// // Top-level modules are contained by the 'Unit'. Non-top-level modules are contained in 'Module's.
if (auto mod = dynamic_pointer_cast<Module>(p); mod && !dynamic_pointer_cast<Unit>(mod->container()))
if (auto mod = dynamic_pointer_cast<Module>(p); mod && !mod->isTopLevel())
{
return "the 'java:package' metadata can only be applied at the file level or to top-level modules";
}
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/slice2js/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,10 +1041,9 @@ Slice::Gen::ExportsVisitor::visitModuleStart(const ModulePtr& p)
const string scoped = getLocalScope(p->scoped());
if (_exportedModules.insert(scoped).second)
{
const bool topLevel = dynamic_pointer_cast<Unit>(p->container()) != nullptr;
_out << sp;
_out << nl;
if (topLevel)
if (p->isTopLevel())
{
if (_importedModules.find(scoped) == _importedModules.end())
{
Expand Down
Loading

0 comments on commit c7a5a9b

Please sign in to comment.