diff --git a/src/Generator/Passes/FixParameterUsageFromComments.cs b/src/Generator/Passes/FixParameterUsageFromComments.cs new file mode 100644 index 00000000..afe36fa1 --- /dev/null +++ b/src/Generator/Passes/FixParameterUsageFromComments.cs @@ -0,0 +1,121 @@ +using CppSharp.AST; +using System; + +namespace CppSharp.Passes +{ + /// + /// This pass will look through semantic comments (Doxygen) in the source code + /// and fix the parameter usage (out or ref) if that information is available. + /// + public class FixParameterUsageFromComments : TranslationUnitPass, ICommentVisitor + { + Method currentMethod; + + public override bool VisitMethodDecl(Method method) + { + if (method.Comment == null || method.Comment.FullComment == null) + return base.VisitMethodDecl(method); + + Method previousMethod = currentMethod; + currentMethod = method; + + var fullComment = method.Comment.FullComment; + VisitFull(fullComment); + + currentMethod = previousMethod; + + return base.VisitMethodDecl(method); + } + + static ParameterUsage GetParameterUsageFromDirection(ParamCommandComment.PassDirection dir) + { + switch (dir) + { + case ParamCommandComment.PassDirection.In: + return ParameterUsage.In; + case ParamCommandComment.PassDirection.InOut: + return ParameterUsage.InOut; + case ParamCommandComment.PassDirection.Out: + return ParameterUsage.Out; + } + + throw new NotSupportedException("Unknown parameter pass direction"); + } + + public bool VisitParamCommand(ParamCommandComment comment) + { + if (comment.Direction == ParamCommandComment.PassDirection.In) + return false; + + if (!comment.IsParamIndexValid) + return false; + + var parameter = currentMethod.Parameters[(int)comment.ParamIndex]; + parameter.Usage = GetParameterUsageFromDirection(comment.Direction); + + return true; + } + + public bool VisitFull(FullComment comment) + { + foreach (var block in comment.Blocks) + block.Visit(this); + + return true; + } + + #region Comments visitor stubs + + public bool VisitBlockCommand(BlockCommandComment comment) + { + return true; + } + + public bool VisitParagraphCommand(ParagraphComment comment) + { + return true; + } + + public bool VisitTParamCommand(TParamCommandComment comment) + { + return true; + } + + public bool VisitVerbatimBlock(VerbatimBlockComment comment) + { + return true; + } + + public bool VisitVerbatimLine(VerbatimLineComment comment) + { + return true; + } + + public bool VisitHTMLStartTag(HTMLStartTagComment comment) + { + return true; + } + + public bool VisitHTMLEndTag(HTMLEndTagComment comment) + { + return true; + } + + public bool VisitText(TextComment comment) + { + return true; + } + + public bool VisitInlineCommand(InlineCommandComment comment) + { + return true; + } + + public bool VisitVerbatimBlockLine(VerbatimBlockLineComment comment) + { + return true; + } + + #endregion + } +} \ No newline at end of file