private void ScanTokens(NClang.ClangTranslationUnit tu, SyntaxHighlightDataList result) { var tokens = tu.Tokenize(tu.GetCursor().CursorExtent); //var annotatedTokens = tokens.Annotate(); //TODO see if this can provide us with additional data. foreach (var token in tokens.Tokens) { var highlightData = new OffsetSyntaxHighlightingData(); highlightData.Start = token.Extent.Start.FileLocation.Offset; highlightData.Length = token.Extent.End.FileLocation.Offset - highlightData.Start; switch (token.Kind) { case TokenKind.Comment: highlightData.Type = HighlightType.Comment; result.Add(highlightData); break; case TokenKind.Keyword: highlightData.Type = HighlightType.Keyword; result.Add(highlightData); break; } } }
private void ScanTokens(NClang.ClangTranslationUnit tu, SyntaxHighlightDataList result) { var tokens = tu.Tokenize(tu.GetCursor().CursorExtent); foreach (var token in tokens.Tokens) { var highlightData = new OffsetSyntaxHighlightingData { Start = token.Extent.Start.FileLocation.Offset }; highlightData.Length = token.Extent.End.FileLocation.Offset - highlightData.Start; switch (token.Kind) { case TokenKind.Comment: highlightData.Type = HighlightType.Comment; result.Add(highlightData); break; case TokenKind.Keyword: highlightData.Type = HighlightType.Keyword; result.Add(highlightData); break; } } }
/// <summary> /// Parse the given source file and the translation unit corresponding /// to that file. /// </summary> /// <remarks> /// This routine is the main entry point for the Clang C API, providing the /// ability to parse a source file into a translation unit that can then be /// queried by other functions in the API. This routine accepts a set of /// command-line arguments so that the compilation can be configured in the same /// way that the compiler is configured on the command line. /// </remarks> /// <param name="sourceFilename"> /// The name of the source file to load, or NULL if the /// source file is included in \p command_line_args. /// </param> /// <param name="commandLineArgs"> /// The command-line arguments that would be /// passed to the clang executable if it were being invoked out-of-process. /// These command-line options will be parsed and will affect how the translation /// unit is parsed. Note that the following options are ignored: '-c', /// '-emit-ast', '-fsyntax-only' (which is the default), and '-o \<output file>'. /// </param> /// <param name="unsavedFiles"> /// the files that have not yet been saved to disk /// but may be required for parsing, including the contents of /// those files. The contents and name of these files (as specified by /// CXUnsavedFile) are copied when necessary, so the client only needs to /// guarantee their validity until the call to this function returns. /// </param> /// <param name="options"> /// A bitmask of options that affects how the translation unit /// is managed but not its compilation. This should be a bitwise OR of the /// <see cref="TranslationUnitFlags"/> flags. /// </param> /// <param name="translationUnit"> /// A new translation unit describing the parsed code and containing /// any diagnostics produced by the compiler. If there is a failure from which /// the compiler cannot recover, returns <c>null</c>. /// </param> /// <returns>An <seealso cref="ErrorCode"/>.</returns> public ErrorCode ParseTranslationUnit(string sourceFilename, string [] commandLineArgs, ClangUnsavedFile [] unsavedFiles, TranslationUnitFlags options, out ClangTranslationUnit translationUnit) { var files = (unsavedFiles ?? new ClangUnsavedFile [0]).Select(u => new CXUnsavedFile(u.FileName, u.Contents)).ToArray(); IntPtr tuptr; ErrorCode error = LibClang.clang_parseTranslationUnit2(Handle, sourceFilename, commandLineArgs, commandLineArgs.Length, files, (uint)files.Length, options, out tuptr); translationUnit = error == ErrorCode.Success ? new ClangTranslationUnit(tuptr) : null; return(error); }
/// <summary> /// Index the given translation unit via callbacks implemented through <see cref="ClangIndexerCallbacks"/>. /// </summary> /// <remarks> /// The order of callback invocations is not guaranteed to be the same as /// when indexing a source file. The high level order will be: /// /// -Preprocessor callbacks invocations /// -Declaration/reference callbacks invocations /// -Diagnostic callback invocations /// </remarks> /// <param name="clientData">data supplied by the client, which will /// be passed to the invoked callbacks.</param> /// <param name="indexCallbacks">Pointer to indexing callbacks that the client /// implements.</param> /// <param name="options">A bitmask of options that affects how indexing is /// performed. This should be a bitwise OR of the <see cref="IndexOptionFlags"/> flags.</param> /// <param name="translationUnit"></param> public void IndexTranslationUnit(IntPtr clientData, ClangIndexerCallbacks [] indexCallbacks, IndexOptionFlags options, ClangTranslationUnit translationUnit) { if (indexCallbacks == null) { throw new ArgumentNullException("indexCallbacks"); } if (translationUnit == null) { throw new ArgumentNullException("translationUnit"); } var cbs = indexCallbacks.Select(ic => ic.ToNative()).ToArray(); var ret = LibClang.clang_indexTranslationUnit(Handle, clientData, cbs, (uint)(cbs.Length * Marshal.SizeOf(typeof(IndexerCallbacks))), options, translationUnit.Handle); if (ret != 0) { throw new ClangServiceException(string.Format("Faied to index translation unit: {0} Reason: {1}", translationUnit.TranslationUnitSpelling, ret)); } }
public bool Equals(ClangTranslationUnit other) { return(other != null && Handle == other.Handle); }