Skip to content

Commit 9949908

Browse files
authored
Merge pull request DeusData#2074 from Tokarzewski/fix/721-vb6-frm-cls-disambiguation
fix(discover): keep VB6 .frm/.cls files out of the FORM and Apex grammars
2 parents a6aea0b + 6db22aa commit 9949908

4 files changed

Lines changed: 129 additions & 4 deletions

File tree

‎src/discover/discover.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ static CBMLanguage detect_file_language(const char *entry_name, const char *abs_
700700
if (dot && strcmp(dot, ".m") == 0) {
701701
lang = cbm_disambiguate_m(abs_path);
702702
}
703-
/* Special: .cls is shared by ObjectScript UDL and Apex */
703+
/* Special: .cls is shared by ObjectScript UDL, Apex and VB6 class modules */
704704
if (dot && strcmp(dot, ".cls") == 0) {
705705
lang = cbm_disambiguate_cls(abs_path);
706706
}
@@ -712,6 +712,10 @@ static CBMLanguage detect_file_language(const char *entry_name, const char *abs_
712712
if (dot && strcmp(dot, ".cfc") == 0) {
713713
lang = cbm_disambiguate_cfc(abs_path);
714714
}
715+
/* Special: .frm is shared by FORM and VB6 forms (#721) */
716+
if (dot && strcmp(dot, ".frm") == 0) {
717+
lang = cbm_disambiguate_frm(abs_path);
718+
}
715719
/* Special: ObjectScript Studio Export XML (<Export generator="...">) is
716720
* detected by content; otherwise .xml stays XML. */
717721
if (lang == CBM_LANG_XML) {

‎src/discover/discover.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ CBMLanguage cbm_disambiguate_m(const char *path);
4141

4242
/* Disambiguate .cls files by reading first 4KB of content.
4343
* Returns CBM_LANG_OBJECTSCRIPT_UDL if a line starts with "Class <Uppercase>",
44+
* CBM_LANG_COUNT (unsupported) for a Visual Basic 6 class module (#721),
4445
* otherwise CBM_LANG_APEX. On read failure, defaults to CBM_LANG_APEX. */
4546
CBMLanguage cbm_disambiguate_cls(const char *path);
4647

48+
/* Disambiguate .frm files by reading first 4KB of content (#721).
49+
* Returns CBM_LANG_COUNT (unsupported) for a Visual Basic 6 form, otherwise
50+
* CBM_LANG_FORM. On read failure, defaults to CBM_LANG_FORM. */
51+
CBMLanguage cbm_disambiguate_frm(const char *path);
52+
4753
/* Disambiguate .inc files by reading first 4KB of content.
4854
* Returns CBM_LANG_OBJECTSCRIPT_ROUTINE if it looks like an ObjectScript
4955
* include (a "ROUTINE <Uppercase>" header), otherwise CBM_LANG_BITBAKE.

‎src/discover/language.c‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,9 +1249,49 @@ CBMLanguage cbm_disambiguate_m(const char *path) {
12491249
return CBM_LANG_MATLAB;
12501250
}
12511251

1252-
/* Disambiguate .cls files: shared by InterSystems ObjectScript UDL and
1253-
* Salesforce Apex. ObjectScript class files begin with a line of the form
1254-
* "Class <UppercasePackage>...". Defaults to Apex on any doubt. */
1252+
/* Visual Basic 6 / VBA source exports are recognisable from their header (#721):
1253+
* every module carries `Attribute VB_Name = "..."`, class modules open with
1254+
* `VERSION 1.0 CLASS`, forms/controls with `VERSION 5.00` + `Begin VB.Form` /
1255+
* `Begin VB.UserControl`. None of these occur in Apex or ObjectScript classes
1256+
* or in FORM programs, whose .cls / .frm extensions VB6 happens to share. */
1257+
static bool has_vb6_markers(const char *buf) {
1258+
return str_contains(buf, "Attribute VB_Name") || str_contains(buf, "VERSION 1.0 CLASS") ||
1259+
str_contains(buf, "Begin VB.") || str_contains(buf, "\nOption Explicit");
1260+
}
1261+
1262+
/* Disambiguate .frm files: shared by the FORM symbolic-manipulation language
1263+
* and Visual Basic 6 forms (#721). There is no Visual Basic language yet, so a
1264+
* VB6 form is reported as unsupported (CBM_LANG_COUNT) rather than handed to
1265+
* the FORM grammar, which yields no defs and stray junk nodes. Defaults to
1266+
* FORM on any doubt (preserves existing behaviour). */
1267+
CBMLanguage cbm_disambiguate_frm(const char *path) {
1268+
if (!path) {
1269+
return CBM_LANG_FORM;
1270+
}
1271+
1272+
FILE *f = cbm_fopen(path, "r");
1273+
if (!f) {
1274+
return CBM_LANG_FORM;
1275+
}
1276+
1277+
char buf[CBM_SZ_4K + SKIP_ONE];
1278+
size_t n = fread(buf, SKIP_ONE, CBM_SZ_4K, f);
1279+
buf[n] = '\0';
1280+
(void)fclose(f);
1281+
1282+
/* VB6 form files open with "VERSION x.yy" on line 1. */
1283+
if (strncmp(buf, "VERSION ", SLEN("VERSION ")) == 0 &&
1284+
isdigit((unsigned char)buf[SLEN("VERSION ")])) {
1285+
return CBM_LANG_COUNT;
1286+
}
1287+
return has_vb6_markers(buf) ? CBM_LANG_COUNT : CBM_LANG_FORM;
1288+
}
1289+
1290+
/* Disambiguate .cls files: shared by InterSystems ObjectScript UDL, Salesforce
1291+
* Apex and Visual Basic 6 class modules (#721). ObjectScript class files begin
1292+
* with a line of the form "Class <UppercasePackage>..."; VB6 class modules
1293+
* carry the VB6 header markers and are reported as unsupported (CBM_LANG_COUNT)
1294+
* until a Visual Basic grammar exists. Defaults to Apex on any doubt. */
12551295
CBMLanguage cbm_disambiguate_cls(const char *path) {
12561296
if (!path) {
12571297
return CBM_LANG_APEX;
@@ -1267,6 +1307,10 @@ CBMLanguage cbm_disambiguate_cls(const char *path) {
12671307
buf[n] = '\0';
12681308
(void)fclose(f);
12691309

1310+
if (has_vb6_markers(buf)) {
1311+
return CBM_LANG_COUNT;
1312+
}
1313+
12701314
const char *line = buf;
12711315
while (*line) {
12721316
if (strncmp(line, "Class ", SLEN("Class ")) == 0 &&

‎tests/test_language.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,72 @@ static CBMLanguage disambiguate_cfc_content(const char *name, const char *conten
640640
return lang;
641641
}
642642

643+
/* ── .cls / .frm: VB6 vs Apex / ObjectScript / FORM (#721) ────────── */
644+
645+
static bool write_probe_file(const char *path, const char *content) {
646+
FILE *f = fopen(path, "w");
647+
if (!f) {
648+
return false;
649+
}
650+
fputs(content, f);
651+
fclose(f);
652+
return true;
653+
}
654+
655+
TEST(lang_cls_vb6_class_module_unsupported) {
656+
char path[256];
657+
snprintf(path, sizeof(path), "%s/test_lang_vb6.cls", cbm_tmpdir());
658+
ASSERT_TRUE(write_probe_file(path, "VERSION 1.0 CLASS\r\nBEGIN\r\n MultiUse = -1 'True\r\n"
659+
"END\r\nAttribute VB_Name = \"Widget\"\r\n"
660+
"Option Explicit\r\n\r\nPublic Sub Go()\r\nEnd Sub\r\n"));
661+
ASSERT_EQ(cbm_disambiguate_cls(path), CBM_LANG_COUNT);
662+
remove(path);
663+
PASS();
664+
}
665+
666+
TEST(lang_cls_apex_stays_apex) {
667+
char path[256];
668+
snprintf(path, sizeof(path), "%s/test_lang_apex.cls", cbm_tmpdir());
669+
ASSERT_TRUE(write_probe_file(path, "public with sharing class Widget {\n"
670+
" public void go() {}\n}\n"));
671+
ASSERT_EQ(cbm_disambiguate_cls(path), CBM_LANG_APEX);
672+
remove(path);
673+
/* Unreadable file keeps the pre-existing owner. */
674+
ASSERT_EQ(cbm_disambiguate_cls("/tmp/nonexistent_file_12345.cls"), CBM_LANG_APEX);
675+
PASS();
676+
}
677+
678+
TEST(lang_cls_objectscript_stays_objectscript) {
679+
char path[256];
680+
snprintf(path, sizeof(path), "%s/test_lang_udl.cls", cbm_tmpdir());
681+
ASSERT_TRUE(write_probe_file(path, "Class MyPkg.Widget Extends %RegisteredObject\n{\n\n"
682+
"Method Go()\n{\n}\n\n}\n"));
683+
ASSERT_EQ(cbm_disambiguate_cls(path), CBM_LANG_OBJECTSCRIPT_UDL);
684+
remove(path);
685+
PASS();
686+
}
687+
688+
TEST(lang_frm_vb6_form_unsupported) {
689+
char path[256];
690+
snprintf(path, sizeof(path), "%s/test_lang_vb6.frm", cbm_tmpdir());
691+
ASSERT_TRUE(write_probe_file(path, "VERSION 5.00\r\nBegin VB.Form Form1 \r\n"
692+
" Caption = \"Hi\"\r\nEnd\r\n"
693+
"Attribute VB_Name = \"Form1\"\r\nOption Explicit\r\n"));
694+
ASSERT_EQ(cbm_disambiguate_frm(path), CBM_LANG_COUNT);
695+
remove(path);
696+
PASS();
697+
}
698+
699+
TEST(lang_frm_form_stays_form) {
700+
char path[256];
701+
snprintf(path, sizeof(path), "%s/test_lang_form.frm", cbm_tmpdir());
702+
ASSERT_TRUE(write_probe_file(path, "Symbols x, y;\nLocal F = x + y;\nPrint;\n.end\n"));
703+
ASSERT_EQ(cbm_disambiguate_frm(path), CBM_LANG_FORM);
704+
remove(path);
705+
ASSERT_EQ(cbm_disambiguate_frm("/tmp/nonexistent_file_12345.frm"), CBM_LANG_FORM);
706+
PASS();
707+
}
708+
643709
TEST(lang_cfc_tag_component) {
644710
/* <cfcomponent> wrapper ⇒ tag dialect. */
645711
ASSERT_EQ(disambiguate_cfc_content("test_cfc_tag.cfc",
@@ -1351,6 +1417,11 @@ SUITE(language) {
13511417
RUN_TEST(lang_cfc_tag_after_license_comment);
13521418
RUN_TEST(lang_cfc_script_after_license_comment);
13531419
RUN_TEST(lang_cfc_default_on_read_fail);
1420+
RUN_TEST(lang_cls_vb6_class_module_unsupported);
1421+
RUN_TEST(lang_cls_apex_stays_apex);
1422+
RUN_TEST(lang_cls_objectscript_stays_objectscript);
1423+
RUN_TEST(lang_frm_vb6_form_unsupported);
1424+
RUN_TEST(lang_frm_form_stays_form);
13541425

13551426
/* Go test ports */
13561427
/* New languages */

0 commit comments

Comments
 (0)