Files
VCCPlusOSS/platforms/macos/Tests/CMOCProjectIndexTests.swift
T

232 lines
16 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import XCTest
final class CMOCProjectIndexTests: XCTestCase {
private func location(_ needle: String, in source: String, occurrence: Int = 0) -> (line: Int, column: Int, offset: Int) {
let text = source as NSString; var start = 0, found = NSRange(location: NSNotFound, length: 0)
for _ in 0...occurrence { found = text.range(of: needle, range: NSRange(location: start, length: text.length - start)); if found.location == NSNotFound { XCTFail("Missing fixture token \(needle)"); return (1,1,0) }; start = NSMaxRange(found) }
let before = text.substring(to: found.location)
return (before.components(separatedBy: "\n").count, found.location - text.lineRange(for: NSRange(location: found.location, length: 0)).location + 1, found.location)
}
func testDeclarationsIgnoreCommentsStringsAndCMOCAssembly() {
let source = """
/* void invented(void) {} */
const char *message = "int fake;";
typedef unsigned int Count;
typedef int (*Handler)(int code);
static Count total;
int (*callback)(int);
int compute(int amount) {
Count local = amount;
asm { bogus int assembly_symbol }
// int comment;
return local;
}
"""
let index = CMOCProjectIndex(documents: ["main.c": source])
XCTAssertEqual(Set(index.outline(path: "main.c").map(\.name)), ["message", "Count", "Handler", "total", "callback", "compute"])
XCTAssertEqual(index.symbols.first { $0.name == "callback" }?.kind, .variable)
XCTAssertEqual(index.symbols.first { $0.name == "Handler" }?.kind, .type)
XCTAssertEqual(index.symbols.first { $0.name == "amount" }?.kind, .parameter)
XCTAssertEqual(index.symbols.first { $0.name == "local" }?.scope, "compute")
XCTAssertFalse(index.symbols.contains { ["invented", "fake", "bogus", "assembly_symbol", "comment"].contains($0.name) })
XCTAssertNil(index.token(atUTF16Offset: location("fake", in: source).offset, in: "main.c"))
XCTAssertNil(index.token(atUTF16Offset: location("comment", in: source).offset, in: "main.c"))
}
func testPrototypeNavigatesToExternalDefinitionAndDuplicateDefinitionsStayVisible() {
let source = "#include \"api.h\"\nint main(void) { return calculate(2); }"
let index = CMOCProjectIndex(documents: ["main.c": source, "api.h": "int calculate(int value);", "compute.c": "int calculate(int value) { return value; }"])
let here = location("calculate", in: source)
let result = index.definitions(named: "calculate", from: "main.c", line: here.line, column: here.column)
XCTAssertEqual(result.map(\.path), ["compute.c"])
XCTAssertTrue(result[0].isDefinition)
XCTAssertFalse(index.symbols.first { $0.path == "api.h" }!.isDefinition)
let duplicate = CMOCProjectIndex(documents: ["main.c": source, "a.c": "int calculate(int v) { return v; }", "b.c": "int calculate(int v) { return v+1; }"])
XCTAssertEqual(duplicate.definitions(named: "calculate", from: "main.c", line: here.line, column: here.column).count, 2)
}
func testLocalAndNestedBlockShadowingStayInsideTheirScope() {
let source = """
int value;
int main(int value) {
value++;
{ int value; value++; }
value++;
}
int other(void) { return value; }
"""
let index = CMOCProjectIndex(documents: ["main.c": source])
let inner = location("value++", in: source, occurrence: 1)
let outer = location("value++", in: source, occurrence: 2)
let other = location("return value", in: source)
XCTAssertEqual(index.definitions(named: "value", from: "main.c", line: inner.line, column: inner.column).first?.line, 4)
XCTAssertEqual(index.definitions(named: "value", from: "main.c", line: outer.line, column: outer.column).first?.kind, .parameter)
XCTAssertEqual(index.definitions(named: "value", from: "main.c", line: other.line, column: other.column + 7).first?.line, 1)
}
func testStaticDefinitionsNeverCrossTranslationUnits() {
let index = CMOCProjectIndex(documents: ["a.c": "static int helper(void) { return 1; }\nint a(void) { return helper(); }", "b.c": "static int helper(void) { return 2; }\nint b(void) { return helper(); }", "c.c": "int c(void) { return helper(); }"])
XCTAssertEqual(index.definitions(named: "helper", from: "a.c", line: 2, column: 22).map(\.path), ["a.c"])
XCTAssertTrue(index.definitions(named: "helper", from: "c.c", line: 1, column: 22).isEmpty)
}
func testLiteralIncludesResolveConfiguredAndBundledHeaderPaths() {
let index = CMOCProjectIndex(documents: [
"src/main.c": "#include \"feature.h\"\n#include <cmoc.h>\nint main(void) { return printFeature(); }",
"include/feature.h": "#include \"detail.h\"\nint printFeature(void);",
"include/detail.h": "typedef unsigned int Count;",
"@toolchain/include/cmoc.h": "int printf(const char *format, ...);",
"unrelated/feature.h": "int printFeature(int unrelated);"
], includePaths: ["include", "@toolchain/include"])
XCTAssertEqual(index.definitions(named: "printFeature", from: "src/main.c", line: 3, column: 25).map(\.path), ["include/feature.h"])
XCTAssertEqual(index.definitions(named: "Count", from: "src/main.c", line: 3, column: 1).map(\.path), ["include/detail.h"])
XCTAssertEqual(index.definitions(named: "printf", from: "src/main.c", line: 3, column: 1).map(\.path), ["@toolchain/include/cmoc.h"])
}
func testPlanetAtlasResolvesCompatibilityConversionsInDefinitionAndCompletion() throws {
let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()
.deletingLastPathComponent().deletingLastPathComponent()
func read(_ path: String) throws -> String { try String(contentsOf: root.appendingPathComponent(path), encoding: .utf8) }
let source = try read("examples/cmoc/manual/atlas.c")
var documents = ["main.c": source, "inc/decbfile.h": try read("third_party/cmoc-libraries/decbfile/decbfile.h")]
for name in ["cmoc", "coco", "stdarg"] {
documents["inc/\(name).h"] = try read("third_party/cmoc/src/stdlib/\(name).h")
}
for name in ["stddef", "stdio", "stdlib", "errno"] {
documents["compat/\(name).h"] = try read("sdk/cmoc/compat/include/\(name).h")
}
let here = location("strtoul(", in: source)
let index = CMOCProjectIndex(documents: documents, includePaths: ["compat", "inc"])
for name in ["strtol", "strtoul"] {
XCTAssertEqual(index.definitions(named: name, from: "main.c", line: here.line, column: here.column).map(\.path), ["compat/stdlib.h"])
let candidates = index.completionSymbols(from: "main.c", offset: here.offset, prefix: name)
XCTAssertEqual(candidates.filter { $0.name == name }.map(\.kind), [.function])
}
}
func testExplicitUndefFollowsOrderedIncludesAndPreservesConditionalAlternatives() {
let source = """
#include "original.h"
int before(void) { return convert(1); }
#include "replacement.h"
int after(void) { return convert(2); }
#include "original.h"
int redefined(void) { return convert(3); }
"""
let docs = ["main.c": source, "original.h": "#define convert(n) old(n)",
"replacement.h": "#undef convert\nint convert(int n);"]
func lookup(_ candidate: String, documents: [String: String] = [:]) -> [CMOCProjectIndex.Symbol] {
let index = CMOCProjectIndex(documents: docs.merging(documents) { _, updated in updated })
let here = location(candidate, in: source)
return index.definitions(named: "convert", from: "main.c", line: here.line, column: here.column)
}
XCTAssertEqual(lookup("convert(1)").map(\.kind), [.macro])
XCTAssertEqual(lookup("convert(2)").map(\.kind), [.function])
XCTAssertEqual(lookup("convert(3)").map(\.kind), [.macro], "An unguarded header may restore the macro")
let maybe = ["replacement.h": "#if UNKNOWN_MODE(9)\n#undef convert\nint convert(int n);\n#endif"]
let uncertain = lookup("convert(2)", documents: maybe)
XCTAssertEqual(uncertain.map(\.kind), [.function, .macro])
XCTAssertTrue(uncertain.allSatisfy(\.conditional))
}
func testKnownInactiveBranchesAreExcludedAndUnknownBranchesRemainMarked() {
let source = """
#if 0
int definitely_off;
#endif
#if ENABLE
int enabled;
#else
int disabled;
#endif
#if UNKNOWN_COMPLEX(2)
int maybe_one;
#else
int maybe_two;
#endif
"""
let index = CMOCProjectIndex(documents: ["main.c": source], defines: ["ENABLE=1"])
XCTAssertEqual(Set(index.symbols.map(\.name)), ["enabled", "maybe_one", "maybe_two"])
XCTAssertFalse(index.symbols.first { $0.name == "enabled" }!.conditional)
XCTAssertTrue(index.symbols.filter { $0.name.hasPrefix("maybe") }.allSatisfy(\.conditional))
let custom = CMOCProjectIndex(documents: ["main.c": "#ifdef CUSTOM_FLAG\nint option;\n#endif"], configurationUncertain: true)
XCTAssertTrue(custom.symbols[0].conditional)
XCTAssertFalse(custom.issues.isEmpty)
}
func testIncludedMacroStateIsConservativeAndMultilineMacrosDoNotCreateFunctions() {
let source = """
#include <flags.h>
#ifdef HEADER_FLAG
int conditional;
#endif
#define REPEAT REPEAT + 1
#define BODY(x) \\
int not_a_function(int y) { return y; }
int real;
"""
let index = CMOCProjectIndex(documents: ["main.c": source])
XCTAssertTrue(index.symbols.first { $0.name == "conditional" }!.conditional)
XCTAssertFalse(index.symbols.contains { $0.name == "not_a_function" })
let macro = index.symbols.first { $0.name == "REPEAT" }!
XCTAssertEqual(macro.column, 9)
XCTAssertEqual(macro.kind, .macro)
}
func testMemberAccessDoesNotPretendSameNamedGlobalIsItsDefinition() {
let source = "struct Record { int value; };\nint value;\nint main(void) { struct Record record; return record.value; }"
let index = CMOCProjectIndex(documents: ["main.c": source])
let here = location("record.value", in: source)
let matches = index.definitions(named: "value", from: "main.c", line: here.line, column: here.column + 7)
XCTAssertEqual(matches.count, 1); XCTAssertEqual(matches[0].kind, .member); XCTAssertTrue(matches[0].conditional)
XCTAssertEqual(index.definitions(named: "value", from: "main.c", line: here.line, column: here.column + 12).first?.kind, .member, "Caret at the end of a member must keep its member context")
}
func testSearchUsesUTF16CoordinatesLiteralMatchingAndExplicitLimits() {
let source = "// 😀 Token token\nconst char *s = \"TOKEN\";\n"
let index = CMOCProjectIndex(documents: ["main.c": source, "@toolchain/include/api.h": "token", "README.md": "token"])
let results = index.search(query: "token", caseSensitive: false, paths: ["main.c"])
XCTAssertEqual(results.matches.count, 3); XCTAssertFalse(results.truncated)
XCTAssertEqual(results.matches[0].column, 7)
XCTAssertEqual((source as NSString).substring(with: NSRange(location: results.matches[0].offsetUTF16, length: results.matches[0].lengthUTF16)), "Token")
XCTAssertEqual(index.search(query: "Token", caseSensitive: true).matches.count, 1)
XCTAssertTrue(index.search(query: "token", limit: 2).truncated)
XCTAssertEqual(index.search(query: ".*").matches.count, 0, "Search is literal, not a regular expression")
}
func testQuotedIncludesTakeLocalPrecedenceAndAngleIncludesUseSearchPaths() {
let index = CMOCProjectIndex(documents: ["main.c": "#include \"api.h\"\n#include <system.h>\nint main(void) { return 0; }", "api.h": "int localAPI(void);", "inc/api.h": "int wrongAPI(void);", "system.h": "int wrongSystem(void);", "inc/system.h": "int configuredSystem(void);"], includePaths: ["inc"])
XCTAssertEqual(index.definitions(named: "localAPI", from: "main.c", line: 3, column: 1).first?.path, "api.h")
XCTAssertFalse(index.definitions(named: "localAPI", from: "main.c", line: 3, column: 1).first!.conditional)
XCTAssertTrue(index.definitions(named: "wrongAPI", from: "main.c", line: 3, column: 1).first!.conditional)
XCTAssertFalse(index.definitions(named: "configuredSystem", from: "main.c", line: 3, column: 1).first!.conditional)
XCTAssertTrue(index.definitions(named: "wrongSystem", from: "main.c", line: 3, column: 1).first!.conditional)
}
func testContinuedCommentsAndStringsDoNotInventDeclarations() {
let source = "// continued \\\nint notCode;\nconst char *text = \"continued \\\n#define NOT_MACRO 1\";\nint actual;"
let index = CMOCProjectIndex(documents: ["main.c": source])
XCTAssertEqual(Set(index.outline(path: "main.c").map(\.name)), ["text", "actual"])
}
func testCMOCAssemblyFunctionsAndForLoopLocalsAreRecognizedWithoutLeaking() {
let source = "asm byte readButtons(void) { ldb #$00; rts; }\nvoid asm output(byte value) { stb $FF20; }\nint i;\nvoid loop(void) { for (int i = 0; i < 2; ++i) { i++; } i++; }"
let index = CMOCProjectIndex(documents: ["main.c": source])
XCTAssertEqual(index.symbols.filter { $0.kind == .function && $0.isDefinition }.map(\.name), ["readButtons", "output", "loop"])
let inside = location("i++", in: source)
let outside = location("i++", in: source, occurrence: 1)
XCTAssertEqual(index.definitions(named: "i", from: "main.c", line: inside.line, column: inside.column).first?.scope, "loop")
XCTAssertNil(index.definitions(named: "i", from: "main.c", line: outside.line, column: outside.column).first?.scope)
XCTAssertFalse(index.symbols.contains { ["ldb", "rts", "stb"].contains($0.name) })
}
func testConditionalInitializersAndFunctionReturningPointerKeepTheirNames() {
let index = CMOCProjectIndex(documents: ["main.c": "int flag; int selected = flag ? 1 : 2; struct Bits { unsigned bit:1; }; int (*factory(void))(int) { return 0; }"])
XCTAssertEqual(index.symbols.first { $0.name == "selected" }?.kind, .variable)
XCTAssertEqual(index.symbols.first { $0.name == "bit" }?.kind, .member)
XCTAssertEqual(index.symbols.first { $0.name == "factory" }?.kind, .function)
XCTAssertTrue(index.symbols.first { $0.name == "factory" }!.isDefinition)
}
func testOversizedInputsAreReportedAndDoNotHideAcceptedDocuments() {
let index = CMOCProjectIndex(documents: ["good.c": "int good;", "large.c": String(repeating: "x", count: CMOCProjectIndex.maximumFileBytes + 1)])
XCTAssertEqual(Set(index.documents.keys), ["good.c"])
XCTAssertEqual(index.symbols.map(\.name), ["good"])
XCTAssertTrue(index.issues.contains { $0.contains("large.c") })
}
func testAggregateTypedefEnumAndMalformedInputRemainBounded() {
let index = CMOCProjectIndex(documents: ["types.h": "typedef struct { int x; } Point;\nenum Color { RED = 1, GREEN, BLUE };\nstruct Named { int y; };", "broken.c": "void incomplete( { int ignored;", "deep.c": String(repeating: "{", count: 80) + "int too_deep;" + String(repeating: "}", count: 80)])
XCTAssertEqual(Set(index.outline(path: "types.h").map(\.name)), ["Point", "Color", "RED", "GREEN", "BLUE", "Named"])
XCTAssertTrue(index.outline(path: "broken.c").isEmpty)
XCTAssertTrue(index.symbols.filter { $0.path == "deep.c" }.isEmpty)
}
}