Skip to content

Commit

Permalink
Merge pull request #26945 from loganharbour/disallow_characters
Browse files Browse the repository at this point in the history
Improve `MooseObject::mooseError()` with more context, correct file paths with many inputs
  • Loading branch information
loganharbour authored Mar 23, 2024
2 parents 4fb62cc + e662172 commit 091245d
Show file tree
Hide file tree
Showing 105 changed files with 1,658 additions and 944 deletions.
44 changes: 23 additions & 21 deletions framework/contrib/hit/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,29 @@ Node::getNodeView()
}

const std::string &
Node::filename()
Node::filename() const
{
return _hnv.node_pool()->stream_name();
}

std::string
Node::fileLocation(const bool with_column /* = true */) const
{
std::stringstream ss;
ss << filename() << ":" << line();
if (with_column)
ss << "." << column();
return ss.str();
}

int
Node::line()
Node::line() const
{
return _hnv.line();
}

int
Node::column()
Node::column() const
{
return _hnv.column();
}
Expand All @@ -253,7 +263,7 @@ Node::floatVal()
valthrow();
}
std::string
Node::strVal()
Node::strVal() const
{
valthrow();
}
Expand All @@ -280,7 +290,7 @@ Node::vecStrVal()
#undef valthrow

NodeType
Node::type()
Node::type() const
{
if (_hnv.type() == wasp::BLANK_LINE)
return NodeType::Blank;
Expand Down Expand Up @@ -319,22 +329,16 @@ Node::children(NodeType t)
return nodes;
}

Node *
Node::parent()
{
return _parent;
}

Node *
Node::root()
{
if (_parent == nullptr)
if (isRoot())
return this;
return _parent->root();
}

std::string
Node::path()
Node::path() const
{
if (!_override_path.empty())
return _override_path;
Expand All @@ -343,7 +347,7 @@ Node::path()
}

std::string
Node::fullpath()
Node::fullpath() const
{
if (_parent == nullptr)
return "";
Expand Down Expand Up @@ -425,9 +429,7 @@ Comment::render(int indent, const std::string & indent_text, int /*maxlen*/)
}

Node *
Comment::clone(bool
absolute_path
)
Comment::clone(bool absolute_path)
{
auto n = new Comment(_dhi, _hnv);
n->setInline(_isinline);
Expand Down Expand Up @@ -468,7 +470,7 @@ Section::clearLegacyMarkers()
}

std::string
Section::path()
Section::path() const
{
return Node::path();
}
Expand Down Expand Up @@ -531,7 +533,7 @@ Field::Field(std::shared_ptr<wasp::DefaultHITInterpreter> dhi, wasp::HITNodeView
}

std::string
Field::path()
Field::path() const
{
return Node::path();
}
Expand Down Expand Up @@ -740,7 +742,7 @@ Field::setVal(const std::string & value, Kind kind)
}

std::string
Field::val()
Field::val() const
{
return extractValue(_hnv.data());
}
Expand Down Expand Up @@ -887,7 +889,7 @@ Field::floatVal()
}

std::string
Field::strVal()
Field::strVal() const
{
const auto value = val();
std::string s = value;
Expand Down
35 changes: 21 additions & 14 deletions framework/contrib/hit/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ enum class NodeType
Comment, /// Represents comments that are not directly part of the actual hit document.
Field, /// Represents field-value pairs (i.e. paramname=val).
Blank, /// Represents a blank line
Other, /// Represents any other type of node
Other, /// Represents any other type of node
};

/// Traversal order for walkers. Determines if the walker on a node is executed before or
Expand Down Expand Up @@ -176,23 +176,26 @@ class Node
wasp::HITNodeView getNodeView();

/// type returns the type of the node (e.g. one of Field, Section, Comment, etc.)
NodeType type();
NodeType type() const;
/// path returns this node's local/direct contribution its full hit path. For section nodes, this
/// is the section name, for field nodes, this is the field/parameter name, for other nodes this
/// is empty. if setOverridePath (wasp) has been called for this Node, then that set path is
/// returned.
virtual std::string path();
virtual std::string path() const;
/// fullpath returns the full hit path to this node (including all parent sections
/// recursively) starting from the tree's root node.
std::string fullpath();
std::string fullpath() const;
/// line returns the line number of the original parsed input (file) that contained the start of
/// the content that this node was built from.
int line();
int line() const;
/// column returns the starting column number of this node in the original parsed input
int column();
int column() const;
/// name returns the file name of the original parsed input (file) that contained the start of
/// the content that this node was built from.
const std::string & filename();
const std::string & filename() const;
/// returns the file location in the form <filename()>:<line()>.<column()> where
/// the column is optional defined by \p with_column
std::string fileLocation(const bool with_column = true) const;

/// the following functions return the stored value of the node (if any exists) of the type
/// indicated in the function name. If the node holds a value of a different type or doesn't hold
Expand All @@ -203,7 +206,7 @@ class Node
/// strVal is special in that it only throws an exception if the node doesn't hold a value at
/// all. All nodes with a value hold data that was originally represented as a string in the
/// parsed input - so this returns that raw string.
virtual std::string strVal();
virtual std::string strVal() const;
/// the vec-prefixed value retrieval functions assume the node holds a string-typed value holding
/// whitespace delimited entries of the element type indicated in the function name.
virtual std::vector<double> vecFloatVal();
Expand All @@ -222,9 +225,14 @@ class Node
/// children returns a list of this node's children of the given type t.
std::vector<Node *> children(NodeType t = NodeType::All);
/// parent returns a pointer to this node's parent node or nullptr if this node has no parent.
Node * parent();
///@{
Node * parent() { return _parent; }
const Node * parent() const { return _parent; }
///@}
/// root returns the root node for the gepot tree this node resides in.
Node * root();
/// Whether or not this is the root
bool isRoot() const { return parent() == nullptr; }
/// clone returns a complete (deep) copy of this node. The caller will be responsible for
/// managing the memory/deallocation of the returned clone node.
virtual Node * clone(bool absolute_path = false) = 0;
Expand Down Expand Up @@ -285,7 +293,6 @@ class Node
wasp::HITNodeView _hnv;

private:

template <typename T>
T paramInner(Node *)
{
Expand Down Expand Up @@ -442,7 +449,7 @@ class Section : public Node
void clearLegacyMarkers();

/// path returns the hit path located in the section's header i.e. the section's name.
virtual std::string path() override;
virtual std::string path() const override;

virtual std::string
render(int indent = 0, const std::string & indent_text = default_indent, int maxlen = 0) override;
Expand Down Expand Up @@ -472,7 +479,7 @@ class Field : public Node
Field(std::shared_ptr<wasp::DefaultHITInterpreter> dhi, wasp::HITNodeView hnv);

/// path returns the hit Field name (i.e. content before the "=")
virtual std::string path() override;
virtual std::string path() const override;

virtual std::string
render(int indent = 0, const std::string & indent_text = default_indent, int maxlen = 0) override;
Expand All @@ -490,7 +497,7 @@ class Field : public Node
void setVal(const std::string & value, Kind kind = Kind::None);
/// val returns the raw text of the field's value as it was read from the hit input. This is
/// the value set by setVal.
std::string val();
std::string val() const;

virtual std::vector<double> vecFloatVal() override;
virtual std::vector<bool> vecBoolVal() override;
Expand All @@ -499,7 +506,7 @@ class Field : public Node
virtual bool boolVal() override;
virtual int64_t intVal() override;
virtual double floatVal() override;
virtual std::string strVal() override;
virtual std::string strVal() const override;

private:
Kind _kind;
Expand Down
5 changes: 3 additions & 2 deletions framework/doc/content/source/interfaces/DataFileInterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ getDataFileNameByName | Finds a data file given a relative path
Files located in `moose/framework/data`, the `moose/modules/*/data`, or
`<your_app>/data` directories can be retrieved using the `getDataFileName(const
std::string & param)` function, where `param` is an input parameter of type
`FileName`
`DataFileName`

`getDataFileName` will search (in this order)
If the provided path is absolute, no searching will take place and the absolute
path will be used. Otherwise, `getDataFileName` will search (in this order)

- relative to the input file
- relative to the running binary in the shared directory (assuming the application is installed)
Expand Down
28 changes: 28 additions & 0 deletions framework/include/actions/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,34 @@ class Action : public MooseBase,
*/
virtual void act() = 0;

/**
* Associates the object's parameters \p params with the input location from this
* Action's parameter with the name \p param_name, if one exists.
*
* For example, you have a parameter in this action of type bool with name "add_mesh".
* You then add an action within this action that creates a mesh if this param is
* true. If you call associateWithParameter("add_mesh", action_params) where
* action_params are the parameters for the action, we then associate that action
* with the "add_mesh" parameter. Therefore, the resulting created mesh will also
* be associated with the "add_mesh" param and any errors that are non-parameter errors
* (i.e., mooseError/mooseWarning) will have the line context of the "add_mesh"
* parameter in this action. The same goes for any errors that are produce within
* the created action.
*/
void associateWithParameter(const std::string & param_name, InputParameters & params) const;

/**
* The same as associateWithParameter() without \p from_params, but instead
* allows you to associate this with another object's parameters instead of the
* parameters from this action.
*
* An example here is when you want to associate the creation of an action with
* an arugment from the aplication.
*/
void associateWithParameter(const InputParameters & from_params,
const std::string & param_name,
InputParameters & params) const;

// The registered syntax for this block if any
std::string _registered_identifier;

Expand Down
13 changes: 13 additions & 0 deletions framework/include/actions/ActionFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ class ActionFactory
*/
bool isRegisteredTask(const std::string & task) const { return _tasks.count(task); }

/**
* @return The InputParameters for the object that is currently being constructed,
* if any.
*
* Can be used to ensure that all Actions are created using the ActionFactory
*/
const InputParameters * currentlyConstructing() const;

private:
template <class T>
static std::shared_ptr<Action> buildAction(const InputParameters & parameters)
Expand All @@ -119,4 +127,9 @@ class ActionFactory

/// The registered tasks
std::set<std::string> _tasks;

/// The object's parameters that are currently being constructed (if any).
/// This is a vector because we create within create, thus the last entry is the
/// one that is being constructed at the moment
std::vector<const InputParameters *> _currently_constructing;
};
11 changes: 9 additions & 2 deletions framework/include/actions/ActionWarehouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ class ActionWarehouse : public ConsoleStreamInterface
const std::string & getMooseAppName();
const std::string & getCurrentTaskName() const { return _current_task; }

/**
* @return The current action that is running, if any
*/
const Action * getCurrentAction() const { return _current_action; }
/**
* @return The name of the current action that is running
*/
std::string getCurrentActionName() const;

/**
Expand Down Expand Up @@ -304,6 +311,8 @@ class ActionWarehouse : public ConsoleStreamInterface
// When executing the actions in the warehouse, this string will always contain
// the current task name
std::string _current_task;
// The current action that is running
Action * _current_action;

//
// data created by actions
Expand All @@ -322,7 +331,5 @@ class ActionWarehouse : public ConsoleStreamInterface
/// Last task to run before (optional) early termination - blank means no early termination.
std::string _final_task;

ActionIterator _act_iter;

const std::list<Action *> _empty_action_list;
};
7 changes: 6 additions & 1 deletion framework/include/actions/CommonOutputAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// MOOSE includes
#include "Action.h"

#include <optional>

/**
* Meta-action for creating common output object parameters
* This action serves two purpose, first it adds common output object
Expand All @@ -35,8 +37,11 @@ class CommonOutputAction : public Action
/**
* Helper method for creating the short-cut actions
* @param object_type String of the object type, i.e., the value of 'type=' in the input file
* @param param_name The name of the input parameter that is responsible for creating, if any
*/
void create(std::string object_type);
void create(std::string object_type,
const std::optional<std::string> & param_name,
const InputParameters * const from_params = nullptr);

/**
* Check if a Console object that outputs to the screen has been defined
Expand Down
Loading

0 comments on commit 091245d

Please sign in to comment.