public void ReadFile(string filePath, List <string> includes) { this.logger.Write(" Parsing "); this.logger.WriteLine(filePath); string[] arr = { "-x", "c++" }; arr = arr.Concat(includes.Select(x => "-I" + x)).ToArray(); CXTranslationUnit translationUnit; CXUnsavedFile unsavedFile; var translationUnitError = clang.parseTranslationUnit2(this.createIndex, filePath, arr, arr.Length, out unsavedFile, 0, 0, out translationUnit); if (translationUnitError != CXErrorCode.CXError_Success) { this.logger.WriteLine("Error: " + translationUnitError); var numDiagnostics = clang.getNumDiagnostics(translationUnit); for (uint i = 0; i < numDiagnostics; ++i) { var diagnostic = clang.getDiagnostic(translationUnit, i); this.logger.WriteLine(clang.getDiagnosticSpelling(diagnostic).ToString()); clang.disposeDiagnostic(diagnostic); } } var structVisitor = new FileVisitor(this.parser, this.leaves, this.links, this.logger); clang.visitChildren(clang.getTranslationUnitCursor(translationUnit), structVisitor.VisitFile, new CXClientData(IntPtr.Zero)); clang.disposeTranslationUnit(translationUnit); }
public CXChildVisitResult VisitClass(CXCursor cursor, CXCursor parent, IntPtr data) { CXCursorKind curKind = clang.getCursorKind(cursor); if (curKind == CXCursorKind.CXCursor_CXXMethod || curKind == CXCursorKind.CXCursor_Constructor || curKind == CXCursorKind.CXCursor_Destructor) { if (this.current != null) { var lines = FileVisitor.NumLines(cursor); this.current.LOC += lines; } } else if (curKind == CXCursorKind.CXCursor_TypeRef || curKind == CXCursorKind.CXCursor_VarDecl || curKind == CXCursorKind.CXCursor_CXXBaseSpecifier || curKind == CXCursorKind.CXCursor_TemplateRef || curKind == CXCursorKind.CXCursor_DeclRefExpr || curKind == CXCursorKind.CXCursor_ClassTemplate) { this.References(FullName(cursor)); } else if (curKind == CXCursorKind.CXCursor_EnumDecl) { if (this.current != null) { this.leaves[FullName(cursor)] = this.current; } } return(CXChildVisitResult.CXChildVisit_Recurse); }
public CXChildVisitResult VisitFile(CXCursor cursor, CXCursor parent, IntPtr data) { var location = clang.getCursorLocation(cursor); if (clang.Location_isInSystemHeader(location) != 0) { return(CXChildVisitResult.CXChildVisit_Continue); } #if false if (false) { CXFile file; uint i, j, k; clang.getFileLocation(location, out file, out i, out j, out k); var fileName = clang.getFileName(file).ToString().Replace('/', System.IO.Path.PathSeparator).Replace('\\', System.IO.Path.PathSeparator); if (this.visited.Contains(fileName)) { return(CXChildVisitResult.CXChildVisit_Continue); } this.visited.Add(fileName); } #endif CXCursorKind curKind = clang.getCursorKind(cursor); if (curKind == CXCursorKind.CXCursor_Namespace) { var previous = this.namespaces; this.namespaces += "." + clang.getCursorSpelling(cursor).ToString(); clang.visitChildren(cursor, this.VisitFile, new CXClientData(IntPtr.Zero)); this.namespaces = previous; } else if (curKind == CXCursorKind.CXCursor_ClassDecl || curKind == CXCursorKind.CXCursor_StructDecl || curKind == CXCursorKind.CXCursor_UnionDecl) { var fullName = FullName(cursor); if (!this.leaves.ContainsKey(fullName)) { var filter = string.Empty; var name = fullName; var i = fullName.LastIndexOf("."); if (i != -1) { name = fullName.Substring(i + 1); filter = fullName.Substring(0, i); } var branch = this.parser.Dependencies.GetPath(filter, "."); var leaf = this.parser.Create(name, fullName, branch); this.SetSourceProvider(leaf, cursor); this.leaves[fullName] = leaf; this.current = leaf; this.links[this.current] = new HashSet <string>(); } else { var leaf = this.leaves[fullName]; this.current = leaf; this.hasChildren = false; clang.visitChildren(cursor, this.VisitChildren, new CXClientData(IntPtr.Zero)); if (this.hasChildren) { this.SetSourceProvider(leaf, cursor); } } clang.visitChildren(cursor, this.VisitClass, new CXClientData(IntPtr.Zero)); this.current = null; } else if (curKind == CXCursorKind.CXCursor_CXXMethod || curKind == CXCursorKind.CXCursor_Constructor || curKind == CXCursorKind.CXCursor_Destructor) { this.current = null; var fullName = FileVisitor.GetClass(cursor); if (fullName.Length == 0) { return(CXChildVisitResult.CXChildVisit_Continue); } if (this.namespaces.Length > 0) { fullName = this.namespaces.Substring(1) + "." + fullName; } if (this.leaves.ContainsKey(fullName)) { this.current = this.leaves[fullName]; clang.visitChildren(cursor, this.VisitClass, new CXClientData(IntPtr.Zero)); this.current = null; } else { this.logger.Write("Cannot find class "); this.logger.WriteLine(fullName); } } else if (curKind != CXCursorKind.CXCursor_UsingDirective && curKind != CXCursorKind.CXCursor_VarDecl) { this.logger.Write("Skipping "); this.logger.WriteLine(curKind.ToString()); } return(CXChildVisitResult.CXChildVisit_Continue); }