Esempio n. 1
0
        private bool DealingWithDiagnostic(CXDiagnostic d)
        {
            // error spelling
            string spelling = clang.getDiagnosticSpelling(d).ToString();

            // category text
            string categoryText = clang.getDiagnosticCategoryText(d).ToString();

            // severity text
            CXDiagnosticSeverity severity = clang.getDiagnosticSeverity(d);
            string severityStr            = ClangTraits.ToString(severity);

            // source location
            CXSourceLocation location = clang.getDiagnosticLocation(d);
            CXFile           file     = new CXFile(IntPtr.Zero);

            clang.getInstantiationLocation(
                location,
                out file,
                out uint line,
                out uint column,
                out uint offset);

            string fileName = clang.getFileName(file).ToString();

            clang.disposeDiagnostic(d);

            string errorString = string.Format("{0}: {1}-{2}, IN {3}, line: {4}, column: {5}",
                                               severityStr, spelling, categoryText, fileName, line, column);

            return(ClangTraits.IsFatal(severity));
        }
        public static string GetFileName(CXCursor cursor)
        {
            CXSourceLocation loc = clang.getCursorLocation(cursor);
            CXFile           cxfile;
            uint             line, column, offset;

            clang.getExpansionLocation(loc, out cxfile, out line, out column, out offset);
            return(clang.getFileName(cxfile).ToString());
        }
Esempio n. 3
0
        private void EncounterError(string msg, CXSourceLocation location)
        {
            Console.Error.WriteLine($"heart-codegen error: {msg}");
            Console.Error.WriteLine($"\tEncountered at: {location.ToString()}");

            System.Diagnostics.Debug.WriteLine($"heart-codegen error: {msg}");
            System.Diagnostics.Debug.WriteLine($"\tEncountered at: {location.ToString()}");

            ErrorCode = 1;
        }
Esempio n. 4
0
            public DecodedLocation(CXSourceLocation loc, Kind kind = Kind.Presumed)
            {
                string file;
                uint   line, column, offset = 0;

                switch (kind)
                {
                case Kind.Expansion: {
                    CXFile f;
                    clang.getExpansionLocation(loc, out f, out line, out column, out offset);
                    file = clang.getFileName(f).ToString();
                }
                break;

                case Kind.Presumed: {
                    CXString f;
                    clang.getPresumedLocation(loc, out f, out line, out column);
                    file = f.ToString();
                }
                break;

                case Kind.Spelling: {
                    CXFile f;
                    clang.getSpellingLocation(loc, out f, out line, out column, out offset);
                    file = clang.getFileName(f).ToString();
                }
                break;

                case Kind.File: {
                    CXFile f;
                    clang.getFileLocation(loc, out f, out line, out column, out offset);
                    file = clang.getFileName(f).ToString();
                }
                break;

                case Kind.Instantiate: {
                    CXFile f;
                    clang.getInstantiationLocation(loc, out f, out line, out column, out offset);
                    file = clang.getFileName(f).ToString();
                }
                break;

                default:
                    throw new NotImplementedException();
                }

                this.File     = file;
                this.FullPath = this.File != null?System.IO.Path.GetFullPath(this.File) : null;

                this.Line   = line;
                this.Column = column;
                this.Offset = offset;
            }
Esempio n. 5
0
        private void SetSourceProvider(Core.Dependency leaf, CXCursor cursor)
        {
            CXSourceRange    extent        = clang.getCursorExtent(cursor);
            CXSourceLocation startLocation = clang.getRangeStart(extent);

            uint startLine = 0, startColumn = 0, offset;

            CXFile file;

            clang.getSpellingLocation(startLocation, out file, out startLine, out startColumn, out offset);

            this.parser.Sources.Create(leaf, new Core.SourceProvider(clang.getFileName(file).ToString()));
        }
Esempio n. 6
0
 /// <summary>
 /// Gets a cursor
 /// </summary>
 /// <param name="fileName">
 /// A <see cref="string"/>: the filename which a Translation Unit (probably containing the cursor) is associated with.
 /// </param>
 /// <param name="location">
 /// A <see cref="MonoDevelop.Ide.Editor.DocumentLocation"/>: the location in the document (named fileName)
 /// </param>
 /// <returns>
 /// A <see cref="CXCursor"/>: the cursor under the location
 /// </returns>
 public CXCursor GetCursor(string fileName, MonoDevelop.Ide.Editor.DocumentLocation location)
 {
     lock (SyncRoot) {
         CXTranslationUnit TU   = translationUnits [fileName];
         CXFile            file = clang.getFile(TU, fileName);
         CXSourceLocation  loc  = clang.getLocation(
             TU,
             file,
             (uint)(location.Line),
             (uint)(location.Column)
             );
         return(clang.getCursor(TU, loc));
     }
 }
Esempio n. 7
0
        private static int NumLines(CXCursor cursor)
        {
            CXSourceRange    extent        = clang.getCursorExtent(cursor);
            CXSourceLocation startLocation = clang.getRangeStart(extent);
            CXSourceLocation endLocation   = clang.getRangeEnd(extent);

            uint startLine = 0, startColumn = 0;
            uint endLine = 0, endColumn = 0, offset;

            CXFile file;

            clang.getSpellingLocation(startLocation, out file, out startLine, out startColumn, out offset);
            clang.getSpellingLocation(endLocation, out file, out endLine, out endColumn, out offset);

            return(1 + (int)endLine - (int)startLine);
        }
Esempio n. 8
0
        private static string GetClass(CXCursor cursor)
        {
            CXSourceRange    extent        = clang.getCursorExtent(cursor);
            CXSourceLocation startLocation = clang.getRangeStart(extent);
            CXSourceLocation endLocation   = clang.getRangeEnd(extent);

            uint startLine = 0, startColumn = 0, offset;

            CXFile file;

            clang.getSpellingLocation(startLocation, out file, out startLine, out startColumn, out offset);

            var fp   = new System.IO.StreamReader(clang.getFileName(file).ToString());
            var line = string.Empty;

            for (var i = 0; i < startLine; ++i)
            {
                line = fp.ReadLine();
            }

            fp.Close();

            if (line.Contains("template"))
            {
                return(string.Empty);
            }

            int index;

            line  = line.Substring(0, line.LastIndexOf('('));
            line  = line.Substring(0, line.LastIndexOf(':') - 1);
            index = line.LastIndexOf(' ');
            if (index != -1)
            {
                line = line.Substring(index + 1);
            }

            index = line.LastIndexOf('\t');
            if (index != -1)
            {
                line = line.Substring(index + 1);
            }

            line = line.Trim();

            return(line.Replace("::", "."));
        }
Esempio n. 9
0
        public Node(CXCursor cursor)
        {
            CXFile           file;
            uint             line;
            uint             column;
            uint             offset;
            CXSourceLocation location = clang.getCursorLocation(cursor);

            clang.getSpellingLocation(location, out file, out line, out column, out offset);
            m_cursor    = cursor;
            m_kind      = clang.getCursorKind(cursor);
            m_type      = clang.getCursorKindSpelling(m_kind).ToString();
            m_name      = clang.getCursorSpelling(cursor).ToString();
            m_full_name = Utils.GetNamespace(cursor) + m_name;
            m_line      = (int)line;
            m_column    = (int)column;
            m_specifier = Utils.GetAccessSpecifier(cursor);
            m_childrens = new List <Node>();
        }
Esempio n. 10
0
        /// <summary>
        /// Translate clang CXSourceLocation to SourceLocation. Contains dealing with UTF-8 Byte order marking.
        /// </summary>
        /// <param name="loc">
        /// A <see cref="CXSourceLocation"/>: a location
        /// </param>
        /// <returns>
        /// A <see cref="SourceLocation"/>: the translated location
        /// </returns>
        public SourceLocation GetSourceLocation(CXSourceLocation loc)
        {
            CXFile file;
            uint   line, column, offset;

            clang.getExpansionLocation(loc, out file, out line, out column, out offset);
            var fileName = GetFileNameString(file);

            CheckForBom(fileName);

            if (IsBomPresentInFile(fileName))
            {
                return(line == 1 ?
                       new SourceLocation(fileName, line, column - 3, offset - 3)
                                                        :
                       new SourceLocation(fileName, line, column, offset - 3));
                //else column is good as it is, only align offset
            }
            return(new SourceLocation(fileName, line, column, offset));
        }
Esempio n. 11
0
        public static CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            if (clang.Location_isFromMainFile(clang.getCursorLocation(cursor)) == 0)
            {
                return(CXChildVisitResult.CXChildVisit_Continue);
            }

            CXSourceLocation location = clang.getCursorLocation(cursor);

            if (clang.Location_isInSystemHeader(location) != 0)
            {
                return(CXChildVisitResult.CXChildVisit_Continue);
            }

            CXCursorKind kind = clang.getCursorKind(cursor);

            if (clang.isDeclaration(kind) > 0)
            {
                Node.Analyze(cursor);
                clang.visitChildren(cursor, Visit, new CXClientData(new IntPtr()));
            }
            return(CXChildVisitResult.CXChildVisit_Continue);
        }
Esempio n. 12
0
 public EntityKey(CXSourceLocation loc)
 {
     this.Loc = loc;
 }
Esempio n. 13
0
 internal static extern CXCursor clang_getCursor(CXTranslationUnit tu, CXSourceLocation loc);
Esempio n. 14
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="source">Native Clang Source Location</param>
 internal ClangSourceLocation(CXSourceLocation source)
 {
     this.Source = source;
 }
Esempio n. 15
0
 private static SourceLocation ExtractLocation(CXSourceLocation loc)
 {
     loc.GetFileLocation(out CXFile cxfile, out uint line, out uint column, out uint offset);
     return(new SourceLocation(cxfile.ToString(), (int)line, (int)column));
 }
Esempio n. 16
0
 internal static extern CXSourceRange clang_getRange(CXSourceLocation start, CXSourceLocation end);
Esempio n. 17
0
        // AST traverse
        private CXChildVisitResult Visitor(CXCursor cursor, CXCursor parent, IntPtr data)
        {
            // skip system headers
            CXSourceLocation location = clang.getCursorLocation(cursor);

            if (clang.Location_isFromMainFile(location) == 0)
            {
                return(CXChildVisitResult.CXChildVisit_Continue);
            }

            // prepare client data
            GCHandle astHandle = (GCHandle)data;
            AST      ast       = astHandle.Target as AST;

            // get visitor
            IASTVisitor visitor = null;

            switch (cursor.kind)
            {
            // dealing with class
            case CXCursorKind.CXCursor_StructDecl:
            case CXCursorKind.CXCursor_ClassDecl:
                visitor = new ClassVisitor(ast);
                break;

            // dealing with enum type
            case CXCursorKind.CXCursor_EnumDecl:
                visitor = new EnumVisitor(ast);
                break;

            // dealing with function
            case CXCursorKind.CXCursor_FunctionDecl:
                visitor = new FunctionVisitor(ast);
                break;

            case CXCursorKind.CXCursor_Namespace:
                // ignore anonymous namespace
                if (clang.Cursor_isAnonymous(cursor) != 0)
                {
                    return(CXChildVisitResult.CXChildVisit_Continue);
                }

                ProcessNamespace(cursor, data);
                return(CXChildVisitResult.CXChildVisit_Continue);

            case CXCursorKind.CXCursor_TypeAliasDecl:
            case CXCursorKind.CXCursor_TypedefDecl:
                //TypeVisitorHelper.GetNativeType(ast, clang.getCursorType(cursor));
                visitor = new TypeVisitor(ast);
                break;

            case CXCursorKind.CXCursor_ClassTemplate:
                visitor = new ClassTemplateVisitor(ast, this, false);
                break;

            case CXCursorKind.CXCursor_ClassTemplatePartialSpecialization:
                visitor = new ClassTemplateVisitor(ast, this, true);
                break;

            case CXCursorKind.CXCursor_FunctionTemplate:
                break;

            default:
                // TODO ... any other
                break;
            }

            if (visitor != null)
            {
                if (!visitor.DoVisit(cursor, parent))
                {
                    return(CXChildVisitResult.CXChildVisit_Break);
                }
            }

            // deep iteratoring in sub visitor
            return(CXChildVisitResult.CXChildVisit_Continue);
        }
Esempio n. 18
0
 static CXSourceLocation()
 {
     NullLocation = Library.clang_getNullLocation();
 }
Esempio n. 19
0
 internal static extern uint clang_equalLocations(CXSourceLocation l1, CXSourceLocation l2);
Esempio n. 20
0
 internal unsafe static extern void clang_getInstantiationLocation(CXSourceLocation location, CXFile* file,
                                     out uint line, out uint column, out uint offset);
Esempio n. 21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SourceLocation"/> class.
 /// </summary>
 /// <param name="sourceLocation">The sourceLocation<see cref="CXSourceLocation"/></param>
 internal SourceLocation(CXSourceLocation sourceLocation)
 {
     this.m_value = sourceLocation;
 }
Esempio n. 22
0
 /// <summary>
 /// Convert to Managed Clang Source Location
 /// </summary>
 /// <param name="loc">Native Clang Source Location</param>
 /// <returns>Managed Clang Source Location</returns>
 internal static ClangSourceLocation ToManaged(this CXSourceLocation loc)
 {
     return(new ClangSourceLocation(loc));
 }