public void FromTranslationUnitParents() { string filename = "ClangCursorTest.FromTranslationUnit.c"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { var cursor = tu.GetCursor(); Assert.AreEqual(AvailabilityKind.Available, cursor.AvailabilityKind, "AvailabilityKind"); Assert.AreEqual(CursorKind.TranslationUnit, cursor.Kind, "Kind"); Assert.AreEqual(LanguageKind.Invalid, cursor.Language, "Language"); Assert.AreEqual(LinkageKind.Invalid, cursor.Linkage, "LinkageKind"); var lp = cursor.LexicalParent; Assert.IsNotNull(lp, "LexicalParent"); Assert.AreEqual(AvailabilityKind.Available, lp.AvailabilityKind, "lp.AvailabilityKind"); Assert.AreEqual(CursorKind.FirstInvalid, lp.Kind, "lp.Kind"); Assert.AreEqual(LanguageKind.Invalid, lp.Language, "lp.Language"); Assert.AreEqual(LinkageKind.Invalid, lp.Linkage, "lp.LinkageKind"); var sp = cursor.SemanticParent; Assert.IsNotNull(sp, "SemanticParent"); Assert.AreEqual(AvailabilityKind.Available, sp.AvailabilityKind, "sp.AvailabilityKind"); Assert.AreEqual(CursorKind.FirstInvalid, sp.Kind, "sp.Kind"); Assert.AreEqual(LanguageKind.Invalid, sp.Language, "sp.Language"); Assert.AreEqual(LinkageKind.Invalid, sp.Linkage, "sp.LinkageKind"); Assert.AreEqual(tu, cursor.TranslationUnit, "TranslationUnit"); var file = cursor.IncludedFile; Assert.IsNull(file, "file"); }, filename); }
public void CodeCompleteAt() { string filename = "ClangCodeCompleteResults.CodeCompleteAt.c"; string code = "int bar () { return 3; } int foo () { return 5; } int main () { return foo () * bar (); }"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { // 38 is before "return" in foo using (var res = tu.CodeCompleteAt(filename, 1, 38, null, CodeCompleteFlags.None)) { Assert.AreEqual(32, res.ResultCount, "ResultCount"); Assert.AreEqual("", res.ContainerUSR, "ContainerUSR"); Assert.AreEqual(CompletionContext.AnyType | CompletionContext.AnyValue | CompletionContext.ObjCInterface, res.Contexts, "Contexts"); var valid = new List <string> (); foreach (var r in res.Results.Where(r => r.CursorKind != CursorKind.NotImplemented)) { valid.Add(string.Join(" ", r.CompletionString.Chunks.Select(c => c.Text))); /* * int i = 0; * Console.Error.WriteLine ("{0} {1} {2}", r.CursorKind, r.CompletionString.Priority, r.CompletionString.ChunkCount); * foreach (var c in r.CompletionString.Chunks) * Console.Error.WriteLine (" - {0} {1} [{2}]", i++, c.Kind, c.Text); */ } Assert.AreEqual(2, valid.Count, "valid.Count"); // bar and foo (but not main, as it is not declared yet) Assert.IsTrue(valid.Contains("int foo ( )"), "valid contains foo"); Assert.IsTrue(valid.Contains("int bar ( )"), "valid contains bar"); Assert.AreEqual(0, res.DiagnosticsCount, "DiagnosticsCount"); } }, filename, code); }
public void TranslationUnitGetLocation() { string filename = "TranslationUnitTest.GetLocation.c"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { var file = tu.GetFile(filename); var loc = tu.GetLocation(file, 5, 4); Assert.AreEqual(5, loc.ExpansionLocation.Line, "ExpansionLocation.Line"); Assert.AreEqual(4, loc.ExpansionLocation.Column, "ExpansionLocation.Column"); Assert.AreEqual(39, loc.ExpansionLocation.Offset, "ExpansionLocation.Offset"); Assert.AreEqual(5, loc.FileLocation.Line, "FileLocation.Line"); Assert.AreEqual(4, loc.FileLocation.Column, "FileLocation.Column"); Assert.AreEqual(39, loc.FileLocation.Offset, "FileLocation.Offset"); Assert.AreEqual(5, loc.InstantiationLocation.Line, "InstantiationLocation.Line"); Assert.AreEqual(4, loc.InstantiationLocation.Column, "InstantiationLocation.Column"); Assert.AreEqual(39, loc.InstantiationLocation.Offset, "InstantiationLocation.Offset"); Assert.AreEqual(true, loc.IsFromMainFile, "IsFromMainFile"); Assert.AreEqual(false, loc.IsInSystemHeader, "IsInSystemHeader"); Assert.AreEqual(5, loc.PresumedLocation.Line, "PresumedLocation.Line"); Assert.AreEqual(4, loc.PresumedLocation.Column, "PresumedLocation.Column"); Assert.AreEqual(5, loc.SpellingLocation.Line, "SpellingLocation.Line"); Assert.AreEqual(4, loc.SpellingLocation.Column, "SpellingLocation.Column"); Assert.AreEqual(39, loc.SpellingLocation.Offset, "SpellingLocation.Offset"); }, filename); }
public void SimpleSemanticIssue() { string filename = "TranslationUnitTest.DiagnosticSet.c"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { Action <ClangDiagnostic, string> test = (d, label) => { Assert.IsNotNull(d, label + "Diagnostic"); Assert.AreEqual(2, d.Category, label + "D.Category"); Assert.AreEqual("Semantic Issue", d.CategoryText, label + "D.CategoryText"); Assert.IsNotNull(d.ChildDiagnostics, label + "D.ChildDiagnostics"); Assert.AreEqual(1, d.ChildDiagnostics.Count, label + "D.ChildDiagnostics.Count"); //Assert.AreEqual ("", d.ChildDiagnostics.Items.First ().CategoryText, label + "D.ChildDiagnostics.Items.First ().CategoryText"); Assert.AreEqual(0, d.FixItCount, label + "D.FixItCount"); Assert.AreEqual(true, d.Location.IsFromMainFile, label + "D.Location.IsFromMainFile"); Assert.AreEqual(false, d.Location.IsInSystemHeader, label + "D.Location.IsInSystemHeader"); Assert.AreEqual("-Wmain-return-type", d.Options.Enable, label + "D.Options.Enable"); Assert.AreEqual("-Wno-main-return-type", d.Options.Disable, label + "D.Options.Disable"); }; test(tu.GetDiagnostic(0), "From TranslationUnit:"); using (var dset = tu.DiagnosticSet) { Assert.AreEqual(1, dset.Count, "Set.Count"); test(dset.Get(0), "From Set:"); } }, filename); }
public void DefaultProperties() { string filename = "TranslationUnitTest.DefaultProperties.c"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { Assert.AreEqual(SaveTranslationUnitFlags.None, tu.DefaultSaveOptions, "DefaultSaveOptions"); Assert.AreEqual(ReparseTranslationUnitFlags.None, tu.DefaultReparseOptions, "DefaultReparseOptions"); Assert.AreEqual(filename, tu.TranslationUnitSpelling, "TranslationUnitSpelling"); Assert.AreEqual(1, tu.DiagnosticCount, "DiagnosticCount"); }, filename); }
public void ResourceUsage() { string filename = "TranslationUnitTest.ResourceUsage.c"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { using (var ru = tu.GetResourceUsage()) { Assert.AreEqual(12, ru.Count, "ResourceUsage.Count"); for (int i = 0; i < ru.Count; i++) { ru.GetEntry(i); } } }, filename); }
public void TranslationUnitGetLocationBeyondRange() { string filename = "TranslationUnitTest.GetLocation.c"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { var file = tu.GetFile(filename); var loc = tu.GetLocation(file, 100, 100); // anything beyond the actual range is shrinked to the max location... Assert.AreEqual(6, loc.ExpansionLocation.Line, "ExpansionLocation.Line"); Assert.AreEqual(2, loc.ExpansionLocation.Column, "ExpansionLocation.Column"); Assert.AreEqual(63, loc.ExpansionLocation.Offset, "ExpansionLocation.Offset"); // For GetLocationForOffset(), I tried some, but cannot predict the results... }, filename); }
public void GetFile() { string filename = "TranslationUnitTest.ResourceUsage.c"; int count = 0; ClangTestHelpers.WithTranslationUnit((idx, tu) => { var f = tu.GetFile(filename); var result = tu.FindIncludesInFile(f, (cursor, range) => { count++; Assert.AreEqual(CursorKind.InclusionDirective, cursor.Kind, "cursor.Kind"); return(VisitorResult.Continue); }); Assert.AreEqual(FindResult.Success, result, "result"); Assert.AreEqual(1, count, "count"); }, filename); }
public void FromTranslationUnit() { string filename = "ClangCursorTest.FromTranslationUnit.c"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { var c = tu.GetCursor(); Assert.AreEqual(-1, c.ArgumentCount, "ArgumentCount"); Assert.AreEqual(AvailabilityKind.Available, c.AvailabilityKind, "AvailabilityKind"); var t = c.CursorType; Assert.IsNull(t, "CursorType"); Assert.AreEqual(CXXAccessSpecifier.Invalid, c.CxxAccessSpecifier, "CxxAccessSpecifier"); Assert.AreEqual(string.Empty, c.DeclObjCTypeEncoding, "DeclObjCTypeEncoding"); Assert.AreEqual(18446744073709551615m, c.EnumConstantDeclUnsignedValue, "EnumConstantDeclUnsignedValue"); Assert.AreEqual(-9223372036854775808m, c.EnumConstantDeclValue, "EnumConstantDeclValue"); t = c.EnumDeclIntegerType; Assert.IsNull(t, "EnumDeclIntegerType"); Assert.AreEqual(-1, c.FieldDeclBitWidth, "FieldDeclBitWidth"); Assert.AreEqual(null, c.IncludedFile, "IncludedFile"); Assert.AreEqual(false, c.IsBitField, "IsBitField"); Assert.AreEqual(false, c.IsVirtualBase, "IsVirtualBase"); Assert.AreEqual(CursorKind.TranslationUnit, c.Kind, "Kind"); Assert.AreEqual(LanguageKind.Invalid, c.Language, "Language"); var cc = c.LexicalParent; Assert.IsNotNull(cc, "LexicalParent"); Assert.AreEqual(LinkageKind.Invalid, c.Linkage, "Linkage"); Assert.AreEqual(0, c.OverloadedDeclarationCount, "OverloadedDeclarationCount"); t = c.ResultType; Assert.IsNull(t, "ResultType"); cc = c.SemanticParent; Assert.IsNotNull(cc, "SemanticParent"); Assert.AreEqual(tu, c.TranslationUnit, "TranslationUnit"); t = c.TypeDefDeclUnderlyingType; Assert.IsNull(t, "TypeDefDeclUnderlyingType"); }, filename); }
public void CodeCompleteAt2() { string filename = "ClangCodeCompleteResults.CodeCompleteAt2.c"; string code = "int bar () { return 3; } int foo () { return 5; } int main () { return foo () * bar (); }"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { // 45 is after "return " in foo using (var res = tu.CodeCompleteAt(filename, 1, 45, null, CodeCompleteFlags.None)) { Assert.AreEqual(6, res.ResultCount, "ResultCount"); Assert.AreEqual("", res.ContainerUSR, "ContainerUSR"); Assert.AreEqual(CompletionContext.AnyValue, res.Contexts, "Contexts"); var valid = new List <string> (); foreach (var r in res.Results.Where(r => r.CursorKind != CursorKind.NotImplemented)) { valid.Add(string.Join(" ", r.CompletionString.Chunks.Select(c => c.Text))); } Assert.AreEqual(2, valid.Count, "valid.Count"); // bar and foo (but not main, as it is not declared yet) Assert.IsTrue(valid.Contains("int foo ( )"), "valid contains foo"); Assert.IsTrue(valid.Contains("int bar ( )"), "valid contains bar"); Assert.AreEqual(0, res.DiagnosticsCount, "DiagnosticsCount"); } }, filename, code); }
public void IndexTranslationUnit() { string filename = "ClangIndexActionTest.IndexTranslationUnit.c"; string code = @"#include <stdio.h> int bar () { return 3; } int foo () { return 5; } int main () { return foo () * bar (); }"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { var a = idx.CreateIndexAction(); var cb = new ClangIndexerCallbacks(); int abortRequested = 0; cb.AbortQuery += (arg) => { abortRequested++; return(false); }; int diagnosticCount = -1; cb.Diagnostic += (arg1, arg2) => diagnosticCount = arg2.Count; bool enteredMainFile = false; cb.EnteredMainFile += (arg1, arg2) => { enteredMainFile = true; return(new ClangIndex.ClientFile(arg2.Handle)); }; // not practically tested here... cb.ImportedAstFile += (arg1, arg2) => new ClangIndex.ClientAstFile(arg2.Address); var refs = new List <string> (); cb.IndexEntityReference += (arg1, arg2) => refs.Add(string.Format("({0}, {1})", arg2.Location.SourceLocation.ExpansionLocation.Line, arg2.Location.SourceLocation.ExpansionLocation.Column)); bool startedTranslationUnit = false; cb.StartedTranslationUnit += (arg) => { startedTranslationUnit = true; return(new ClangIndex.ContainerInfo(tu.Handle)); }; int includeCount = 0; cb.PreprocessIncludedFile += (arg1, arg2) => { includeCount++; return(new ClangIndex.ClientFile(arg2.Handle)); }; cb.IndexDeclaration += (arg1, arg2) => { if (arg2.Location.FileLocation.File.FileName != filename) { return; } Assert.AreEqual(2, arg2.Location.SourceLocation.ExpansionLocation.Line, "Line: " + arg2.Location.SourceLocation); int col = arg2.Location.SourceLocation.ExpansionLocation.Column; var ent = arg2.EntityInfo; switch (col) { case 5: Assert.AreEqual("bar", ent.Name, "EntityInfo.Name." + col); Assert.AreEqual("c:@F@bar", ent.USR, "EntityInfo.USR." + col); goto case -1; case 30: Assert.AreEqual("foo", ent.Name, "EntityInfo.Name." + col); Assert.AreEqual("c:@F@foo", ent.USR, "EntityInfo.USR." + col); goto case -1; case 55: Assert.AreEqual("main", ent.Name, "EntityInfo.Name." + col); Assert.AreEqual("c:@F@main", ent.USR, "EntityInfo.USR." + col); goto case -1; case -1: Assert.AreEqual(0, arg2.Attributes.Count(), "Count." + col); Assert.IsTrue(arg2.IsContainer, "IsContainer." + col); Assert.IsTrue(arg2.IsDefinition, "IsDefinition." + col); Assert.IsFalse(arg2.IsImplicit, "IsImplicit." + col); Assert.IsFalse(arg2.IsRedeclaration, "IsRedeclaration." + col); Assert.AreEqual(IndexDeclInfoFlags.None, arg2.Flags, "Flags." + col); Assert.IsNotNull(ent, "EntityInfo." + col); Assert.AreEqual(0, ent.AttributeCount, "EntityInfo.AttributeCount." + col); Assert.AreEqual(IndexEntityCxxTemplateKind.NonTemplate, ent.CxxTemplateKind, "EntityInfo.CxxTemplateKind." + col); Assert.AreEqual(IndexEntityLanguage.C, ent.EntityLanguage, "EntityInfo.EntityLanguage." + col); Assert.AreEqual(IndexEntityKind.Function, ent.Kind, "EntityInfo.Kind." + col); Assert.IsNotNull(arg2.Cursor, "Cursor." + col); var dc = arg2.DeclarationAsContainer; Assert.IsNotNull(dc, "DeclarationAsContainer." + col); Assert.AreEqual(arg2.Cursor, dc.Cursor, "DeclarationAsContainer.Cursor." + col); // everything is at top level in this sample. var lc = arg2.LexicalContainer; Assert.IsNotNull(lc, "LexicalContainer." + col); Assert.AreEqual(tu.GetCursor(), lc.Cursor, "LexicalContainer.Cursor." + col); var sc = arg2.SemanticContainer; Assert.IsNotNull(sc, "SemanticContainer." + col); Assert.AreEqual(tu.GetCursor(), lc.Cursor, "SemanticContainer.Cursor." + col); Assert.IsNotNull(arg2.CxxClassDeclaration, "CxxClassDeclaration." + col); Assert.IsNotNull(arg2.ObjCCategoryDeclaration, "ObjCCategoryDeclaration." + col); Assert.IsNotNull(arg2.ObjCContainerDeclaration, "ObjCContainerDeclaration." + col); Assert.IsNotNull(arg2.ObjCInterfaceDeclaration, "ObjCInterfaceDeclaration." + col); Assert.IsNotNull(arg2.ObjCPropertyDeclaration, "ObjCPropertyDeclaration." + col); Assert.IsNotNull(arg2.ObjCProtocolReferenceListDeclaration, "ObjCProtocolReferenceListDeclaration." + col); break; default: Assert.Fail("Unexpected index decl: " + arg2.Location.SourceLocation); break; } }; a.IndexTranslationUnit(IntPtr.Zero, new ClangIndexerCallbacks[] { cb }, IndexOptionFlags.None, tu); Assert.IsTrue(startedTranslationUnit, "startedTranslationUnit"); Assert.IsTrue(enteredMainFile, "enteredMainFile"); Assert.IsTrue(abortRequested > 0, "abortRequested > 0"); Assert.AreEqual(1, diagnosticCount, "diagnosticCount"); Assert.IsTrue(refs.Contains("(2, 72)"), "Entity Reference Indexed: (2, 72)"); Assert.IsTrue(refs.Contains("(2, 81)"), "Entity Reference Indexed: (2, 81)"); Assert.IsTrue(includeCount > 0, "includeCount > 0"); }, filename, code); }
public void Tokenize() { Result [] results = new Result [] { new Result(TokenKind.Keyword, "int", 1, 1, 4), new Result(TokenKind.Identifier, "bar", 1, 5, 8), new Result(TokenKind.Punctuation, "(", 1, 9, 10), new Result(TokenKind.Punctuation, ")", 1, 10, 11), new Result(TokenKind.Punctuation, "{", 1, 12, 13), new Result(TokenKind.Keyword, "return", 1, 14, 20), new Result(TokenKind.Literal, "3", 1, 21, 22), new Result(TokenKind.Punctuation, ";", 1, 22, 23), new Result(TokenKind.Punctuation, "}", 1, 24, 25), new Result(TokenKind.Keyword, "int", 1, 26, 29), new Result(TokenKind.Identifier, "foo", 1, 30, 33), new Result(TokenKind.Punctuation, "(", 1, 34, 35), new Result(TokenKind.Punctuation, ")", 1, 35, 36), new Result(TokenKind.Punctuation, "{", 1, 37, 38), new Result(TokenKind.Keyword, "return", 1, 39, 45), new Result(TokenKind.Literal, "5", 1, 46, 47), new Result(TokenKind.Punctuation, ";", 1, 47, 48), new Result(TokenKind.Punctuation, "}", 1, 49, 50), new Result(TokenKind.Keyword, "int", 1, 51, 54), new Result(TokenKind.Identifier, "main", 1, 55, 59), new Result(TokenKind.Punctuation, "(", 1, 60, 61), new Result(TokenKind.Punctuation, ")", 1, 61, 62), new Result(TokenKind.Punctuation, "{", 1, 63, 64), new Result(TokenKind.Keyword, "return", 1, 65, 71), new Result(TokenKind.Identifier, "foo", 1, 72, 75), new Result(TokenKind.Punctuation, "(", 1, 76, 77), new Result(TokenKind.Punctuation, ")", 1, 77, 78), new Result(TokenKind.Punctuation, "*", 1, 79, 80), new Result(TokenKind.Identifier, "bar", 1, 81, 84), new Result(TokenKind.Punctuation, "(", 1, 85, 86), new Result(TokenKind.Punctuation, ")", 1, 86, 87), new Result(TokenKind.Punctuation, ";", 1, 87, 88), new Result(TokenKind.Punctuation, "}", 1, 89, 90), }; string filename = "TranslationUnitTest.DefaultProperties.c"; string code = "int bar () { return 3; } int foo () { return 5; } int main () { return foo () * bar (); }"; ClangTestHelpers.WithTranslationUnit((idx, tu) => { var ts = tu.Tokenize(tu.GetCursor().CursorExtent); var tokens = ts.Tokens.ToArray(); /* * foreach (var t in ts.Tokens) { * Console.Error.WriteLine ("--------"); * foreach (var pi in t.GetType ().GetProperties ()) * Console.Error.WriteLine (" {0}: {1}", pi, pi.GetValue (t, null)); * } */ for (int i = 0; i < Math.Min(tokens.Length, results.Length); i++) { var r = results [i]; var tok = tokens [i]; Assert.AreEqual(r.Kind, tok.Kind, "Kind." + i); Assert.AreEqual(r.Spelling, tok.Spelling, "Spelling." + i); Assert.AreEqual(r.Line, tok.Location.FileLocation.Line, "Line." + i); Assert.AreEqual(r.Start, tok.Location.FileLocation.Column, "Start." + i); Assert.AreEqual(r.End, tok.Extent.End.FileLocation.Column, "End." + i); } Assert.AreEqual(results.Length, tokens.Length, "result count"); }, filename, code); }