<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;

namespace CppSharp.Passes
{
    /// <summary>
    /// This pass implements checks for helper macros in declarations.
    /// The supported macros are listed below with a "CS" prefix which
    /// stands for CppSharp. Using custom prefixes is also supported by
    /// passing the value to the constructor of the pass.
    /// 
    ///     CS_IGNORE_FILE (translation units)
    ///         Used to ignore whole translation units.
    /// 
    ///     CS_IGNORE (declarations)
    ///         Used to ignore declarations from being processed.
    /// 
    ///     CS_IGNORE_GEN (declarations)
    ///         Used to ignore declaration from being generated.
    /// 
    ///     CS_VALUE_TYPE (classes and structs)
    ///         Used to flag that a class or struct is a value type.
    /// 
    ///     CS_IN_OUT / CS_OUT (parameters)
    ///         Used in function parameters to specify their usage kind.
    /// 
    ///     CS_FLAGS (enums)
    ///         Used to specify that enumerations represent bitwise flags.
    /// 
    ///     CS_READONLY (fields and properties)
    ///         Used in fields and properties to specify read-only semantics.
    /// 
    ///     CS_EQUALS / CS_HASHCODE (methods)
    ///         Used to flag method as representing the .NET Equals or
    ///         Hashcode methods.
    /// 
    /// There isn't a standardized header provided by CppSharp so you will
    /// have to define these on your own.
    /// </summary>
    public class CheckMacroPass : TranslationUnitPass
    {
        private readonly string Prefix;

        public CheckMacroPass(string prefix = "CS")
        {
            Prefix = prefix;
        }

        public override bool VisitDeclaration(Declaration decl)
        {
            if (AlreadyVisited(decl))
                return false;

            if (decl is DeclarationContext && !(decl is Class))
                return true;

            var expansions = decl.PreprocessedEntities.OfType<MacroExpansion>();

            CheckIgnoreMacros(decl, expansions);
            return true;
        }

        void CheckIgnoreMacros(Declaration decl, IEnumerable<MacroExpansion> expansions)
        {
            if (expansions.Any(e => e.Text == Prefix + "_IGNORE" &&
                                    e.Location != MacroLocation.ClassBody &&
                                    e.Location != MacroLocation.FunctionBody &&
                                    e.Location != MacroLocation.FunctionParameters))
                decl.ExplicityIgnored = true;

            if (expansions.Any(e => e.Text == Prefix + "_IGNORE_GEN" &&
                                    e.Location != MacroLocation.ClassBody &&
                                    e.Location != MacroLocation.FunctionBody &&
                                    e.Location != MacroLocation.FunctionParameters))
                decl.GenerationKind = GenerationKind.Link;
        }

        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            var expansions = unit.PreprocessedEntities.OfType<MacroExpansion>();

            if (expansions.Any(e => e.Text == Prefix + "_IGNORE_FILE"))
            {
                unit.ExplicityIgnored = true;
            }

            return base.VisitTranslationUnit(unit);
        }

        public override bool VisitClassDecl(Class @class)
        {<commit_msg>Changed CS_IGNORE_GEN to set generation kind to internal.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;

namespace CppSharp.Passes
{
    /// <summary>
    /// This pass implements checks for helper macros in declarations.
    /// The supported macros are listed below with a "CS" prefix which
    /// stands for CppSharp. Using custom prefixes is also supported by
    /// passing the value to the constructor of the pass.
    /// 
    ///     CS_IGNORE_FILE (translation units)
    ///         Used to ignore whole translation units.
    /// 
    ///     CS_IGNORE (declarations)
    ///         Used to ignore declarations from being processed.
    /// 
    ///     CS_IGNORE_GEN (declarations)
    ///         Used to ignore declaration from being generated.
    /// 
    ///     CS_VALUE_TYPE (classes and structs)
    ///         Used to flag that a class or struct is a value type.
    /// 
    ///     CS_IN_OUT / CS_OUT (parameters)
    ///         Used in function parameters to specify their usage kind.
    /// 
    ///     CS_FLAGS (enums)
    ///         Used to specify that enumerations represent bitwise flags.
    /// 
    ///     CS_READONLY (fields and properties)
    ///         Used in fields and properties to specify read-only semantics.
    /// 
    ///     CS_EQUALS / CS_HASHCODE (methods)
    ///         Used to flag method as representing the .NET Equals or
    ///         Hashcode methods.
    /// 
    /// There isn't a standardized header provided by CppSharp so you will
    /// have to define these on your own.
    /// </summary>
    public class CheckMacroPass : TranslationUnitPass
    {
        private readonly string Prefix;

        public CheckMacroPass(string prefix = "CS")
        {
            Prefix = prefix;
        }

        public override bool VisitDeclaration(Declaration decl)
        {
            if (AlreadyVisited(decl))
                return false;

            if (decl is DeclarationContext && !(decl is Class))
                return true;

            var expansions = decl.PreprocessedEntities.OfType<MacroExpansion>();

            CheckIgnoreMacros(decl, expansions);
            return true;
        }

        void CheckIgnoreMacros(Declaration decl, IEnumerable<MacroExpansion> expansions)
        {
            if (expansions.Any(e => e.Text == Prefix + "_IGNORE" &&
                                    e.Location != MacroLocation.ClassBody &&
                                    e.Location != MacroLocation.FunctionBody &&
                                    e.Location != MacroLocation.FunctionParameters))
                decl.ExplicityIgnored = true;

            if (expansions.Any(e => e.Text == Prefix + "_IGNORE_GEN" &&
                                    e.Location != MacroLocation.ClassBody &&
                                    e.Location != MacroLocation.FunctionBody &&
                                    e.Location != MacroLocation.FunctionParameters))
                decl.GenerationKind = GenerationKind.Internal;
        }

        public override bool VisitTranslationUnit(TranslationUnit unit)
        {
            var expansions = unit.PreprocessedEntities.OfType<MacroExpansion>();

            if (expansions.Any(e => e.Text == Prefix + "_IGNORE_FILE"))
            {
                unit.ExplicityIgnored = true;
            }

            return base.VisitTranslationUnit(unit);
        }

        public override bool VisitClassDecl(Class @class)
        {
{'fraction_non_alphanumeric': 0.08954154727793696, 'fraction_numerical': 0.0, 'mean_word_length': 1.6273993225442227, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4612551', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1680, 'n_words': 515}