コード例 #1
0
        private unsafe void VisitMacroDefinitionRecord(MacroDefinitionRecord macroDefinitionRecord)
        {
            if (IsExcluded(macroDefinitionRecord))
            {
                return;
            }

            if (macroDefinitionRecord.IsFunctionLike)
            {
                AddDiagnostic(DiagnosticLevel.Warning, $"Function like macro definition records are not supported: '{macroDefinitionRecord.Name}'. Generated bindings may be incomplete.", macroDefinitionRecord);
                return;
            }

            var translationUnitHandle = macroDefinitionRecord.TranslationUnit.Handle;
            var tokens = translationUnitHandle.Tokenize(macroDefinitionRecord.Extent).ToArray();

            if ((tokens[0].Kind == CXTokenKind.CXToken_Identifier) && (tokens[0].GetSpelling(translationUnitHandle).CString == macroDefinitionRecord.Spelling))
            {
                if (tokens.Length == 1)
                {
                    // Nothing to do for simple macro definitions with no value
                    return;
                }

                var macroName = $"ClangSharpMacro_{macroDefinitionRecord.Spelling}";

                _ = _fileContentsBuilder.Append('\n');
                _ = _fileContentsBuilder.Append($"const auto {macroName} = ");

                var sourceRangeEnd   = tokens[^ 1].GetExtent(translationUnitHandle).End;
コード例 #2
0
        internal static new PreprocessedEntity Create(CXCursor handle)
        {
            PreprocessedEntity result;

            switch (handle.Kind)
            {
            case CXCursorKind.CXCursor_MacroDefinition:
            {
                result = new MacroDefinitionRecord(handle);
                break;
            }

            case CXCursorKind.CXCursor_MacroExpansion:
            {
                result = new MacroExpansion(handle);
                break;
            }

            case CXCursorKind.CXCursor_PreprocessingDirective:
            {
                result = new PreprocessingDirective(handle);
                break;
            }

            case CXCursorKind.CXCursor_InclusionDirective:
            {
                result = new InclusionDirective(handle);
                break;
            }

            default:
            {
                Debug.WriteLine($"Unhandled preprocessing kind: {handle.KindSpelling}.");
                Debugger.Break();

                result = new PreprocessedEntity(handle, handle.Kind);
                break;
            }
            }

            return(result);
        }
        private unsafe void VisitMacroDefinitionRecord(MacroDefinitionRecord macroDefinitionRecord)
        {
            var translationUnitHandle = macroDefinitionRecord.TranslationUnit.Handle;
            var tokens = translationUnitHandle.Tokenize(macroDefinitionRecord.Extent).ToArray();

            if (macroDefinitionRecord.IsFunctionLike)
            {
                AddDiagnostic(DiagnosticLevel.Warning, $"Function like macro definition records are not supported: '{macroDefinitionRecord.Name}'. Generated bindings may be incomplete.", macroDefinitionRecord);
            }
            else if ((tokens[0].Kind == Interop.CXTokenKind.CXToken_Identifier) && (tokens[0].GetSpelling(translationUnitHandle).CString == macroDefinitionRecord.Spelling))
            {
                if (tokens.Length == 1)
                {
                    // Nothing to do for simple macro definitions with no value
                    return;
                }

                var macroName = $"ClangSharpMacro_{macroDefinitionRecord.Spelling}";

                _fileContentsBuilder.Append('\n');
                _fileContentsBuilder.Append($"const auto {macroName} = ");

                var sourceRangeEnd   = tokens[tokens.Length - 1].GetExtent(translationUnitHandle).End;
                var sourceRangeStart = tokens[1].GetLocation(translationUnitHandle);

                var sourceRange = CXSourceRange.Create(sourceRangeStart, sourceRangeEnd);

                var macroValue = GetSourceRangeContents(translationUnitHandle, sourceRange);
                _fileContentsBuilder.Append(macroValue);

                _fileContentsBuilder.Append(';');
                _fileContentsBuilder.Append('\n');
            }
            else
            {
                AddDiagnostic(DiagnosticLevel.Error, $"Unsupported macro definition record: {macroDefinitionRecord.Name}. Generated bindings may be incomplete.", macroDefinitionRecord);
            }
        }