Repository: Konstantin8105/c4go
Pull request #414 (2019-04-19 21:33:08)

kilo editor : fix

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/414?src=pr&el=h1) Report
> Merging [#414](https://codecov.io/gh/Konstantin8105/c4go/pull/414?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/eab71d298f6865f828127fa0f1ca892b1c36f4fc?src=pr&el=desc) will **decrease** coverage by `0.04%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/414/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/414?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #414      +/-   ##
==========================================
- Coverage   84.99%   84.95%   -0.05%     
==========================================
  Files         191      191              
  Lines       11421    11430       +9     
==========================================
+ Hits         9707     9710       +3     
- Misses       1237     1242       +5     
- Partials      477      478       +1
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/414?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [debug.go](https://codecov.io/gh/Konstantin8105/c4go/pull/414/diff?src=pr&el=tree#diff-ZGVidWcuZ28=) | `87.28% <100%> (+0.61%)` | :arrow_up: |
| [types/sizeof.go](https://codecov.io/gh/Konstantin8105/c4go/pull/414/diff?src=pr&el=tree#diff-dHlwZXMvc2l6ZW9mLmdv) | `70.28% <100%> (-2.71%)` | :arrow_down: |
| [preprocessor/preprocessor.go](https://codecov.io/gh/Konstantin8105/c4go/pull/414/diff?src=pr&el=tree#diff-cHJlcHJvY2Vzc29yL3ByZXByb2Nlc3Nvci5nbw==) | `82.45% <0%> (-0.59%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/414?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/414?src=pr&el=footer). Last update [eab71d2...a25d81d](https://codecov.io/gh/Konstantin8105/c4go/pull/414?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/debug.go b/debug.go
index 1a42572..635aae9 100644
--- a/debug.go
+++ b/debug.go
@@ -39,11 +39,11 @@ func (f compount) Inject(lines [][]byte, filePP preprocessor.FilePP) error {
 
 	// compare line of code
 	{
-		buf, err := filePP.GetSnippet(f.pos.File, f.pos.Line, f.pos.Line, 0, 100000)
+		buf, err := filePP.GetSnippet(f.pos.File, f.pos.Line, f.pos.Line, 0, f.pos.Column)
 		if err != nil {
 			return err
 		}
-		if !bytes.Equal(lines[f.pos.Line-1], buf) {
+		if !bytes.Equal(lines[f.pos.Line-1][:f.pos.Column], buf) {
 			return fmt.Errorf("lines in source and pp source is not equal")
 		}
 	}
@@ -231,21 +231,31 @@ func generateDebugCCode(args ProgramArgs, lines []string, filePP preprocessor.Fi
 			//   |-...
 			//
 			// walking by tree
+			addCompount := func(name string, node ast.Node) {
+				sl, _ := funcPoses[node.Position().File]
+				sl = append(sl, compount{name: name, pos: node.Position()})
+				funcPoses[node.Position().File] = sl
+			}
 			var walk func(node ast.Node)
 			walk = func(node ast.Node) {
 				if node == nil {
 					return
 				}
-				if comp, ok := node.(*ast.CompoundStmt); ok {
-					p := compount{
-						name: "CompoundStmt",
-						pos:  comp.Position(),
-					}
-					sl, _ := funcPoses[comp.Position().File]
-					sl = append(sl, p)
-					funcPoses[comp.Position().File] = sl
+				if _, ok := node.(*ast.CompoundStmt); ok {
+					addCompount("CompoundStmt", node)
 				}
 				for i := range node.Children() {
+					if _, ok := node.Children()[i].(*ast.CompoundStmt); ok {
+						chi := node.Children()[i]
+						switch node.(type) {
+						case *ast.IfStmt:
+							addCompount("if", chi)
+						case *ast.ForStmt:
+							addCompount("for", chi)
+						case *ast.WhileStmt:
+							addCompount("while", chi)
+						}
+					}
 					walk(node.Children()[i])
 				}
 			}
diff --git a/types/sizeof.go b/types/sizeof.go
index efe8b2f..955cb19 100644
--- a/types/sizeof.go
+++ b/types/sizeof.go
@@ -87,7 +87,10 @@ func SizeOf(p *program.Program, cType string) (size int, err error) {
 
 		last := 0
 
-		for _, t := range s.Fields {
+		for k := 0; k < len(s.FieldNames); k++ {
+			fn := s.FieldNames[k]
+			t := s.Fields[fn]
+
 			var bytes int
 			var err error
 
@@ -114,7 +117,8 @@ func SizeOf(p *program.Program, cType string) (size int, err error) {
 			}
 
 			if last != new_par {
-				totalBytes = (totalBytes/pointerSize + 1) * pointerSize
+				var deli float64 = float64(totalBytes)/float64(pointerSize) + 1
+				totalBytes = int(deli) * pointerSize
 			}
 
 			totalBytes += bytes
@@ -145,7 +149,10 @@ func SizeOf(p *program.Program, cType string) (size int, err error) {
 			return 0, fmt.Errorf("error in union")
 		}
 
-		for _, t := range s.Fields {
+		for k := 0; k < len(s.FieldNames); k++ {
+			fn := s.FieldNames[k]
+			t := s.Fields[fn]
+
 			var bytes int
 
 			switch f := t.(type) {
@@ -237,22 +244,16 @@ func SizeOf(p *program.Program, cType string) (size int, err error) {
 	}
 
 	// Get size for array types like: `base_type [count]`
-	totalArraySize := 1
 	arrayType, arraySize := GetArrayTypeAndSize(cType)
 	if arraySize <= 0 {
 		return 0, nil
 	}
 
-	for arraySize != -1 {
-		totalArraySize *= arraySize
-		arrayType, arraySize = GetArrayTypeAndSize(arrayType)
-	}
-
 	baseSize, err := SizeOf(p, arrayType)
 	if err != nil {
 		return 0, fmt.Errorf("error in sizeof baseSize for `%v`",
 			arrayType)
 	}
 
-	return baseSize * totalArraySize, nil
+	return baseSize * arraySize, nil
 }


Test patch:
diff --git a/tests/array.c b/tests/array.c
index 111b584..f871705 100644
--- a/tests/array.c
+++ b/tests/array.c
@@ -734,9 +734,53 @@ void test_post_pointer()
     }
 }
 
+struct po_ni {
+	int * fl;
+};
+
+void test_array_nil()
+{
+	struct po_ni ss;
+	ss.fl = NULL;
+	struct po_ni *s = &ss;
+	if (s->fl) {
+		fail("array_nil");
+	} else {
+		pass("array_nil");
+	}
+	if (s->fl == NULL) {
+		pass("array_nil");
+	} else {
+		fail("array_nil");
+	}
+	if (s->fl != NULL) {
+		fail("array_nil");
+	} else {
+		pass("array_nil");
+	}
+
+	int y = 42;
+	ss.fl = &y;
+	if (s->fl) {
+		pass("array_nil");
+	} else {
+		fail("array_nil");
+	}
+	if (s->fl == NULL) {
+		fail("array_nil");
+	} else {
+		pass("array_nil");
+	}
+	if (s->fl != NULL) {
+		pass("array_nil");
+	} else {
+		fail("array_nil");
+	}
+}
+
 int main()
 {
-    plan(199);
+    plan(205);
 
     test_parg_struct();
     START_TEST(struct_init);
@@ -1276,6 +1320,7 @@ int main()
     START_TEST(negative_index);
     START_TEST(matrix_init);
     START_TEST(post_pointer);
+	START_TEST(array_nil);
 
     done_testing();
 }
diff --git a/tests/sizeof.c b/tests/sizeof.c
index b780305..ac51dcc 100644
--- a/tests/sizeof.c
+++ b/tests/sizeof.c
@@ -108,9 +108,62 @@ typedef struct part3_erow{
 	unsigned char * part;
 } part3_erow;
 
+struct editorSyntax {
+    char **filematch;
+    char **keywords;
+    char singleline_comment_start[2];
+    char multiline_comment_start[3];
+    char multiline_comment_end[3];
+    int flags;
+};
+
+/* C / C++ */
+char *C_HL_extensions[] = {".c",".cpp",NULL};
+char *C_HL_keywords[] = {
+        /* A few C / C++ keywords */
+        "switch","if","while","for","break","continue","return","else",
+        "struct","union","typedef","static","enum","class",
+        /* C types */
+        "int|","long|","double|","float|","char|","unsigned|","signed|",
+        "void|",NULL
+};
+
+#define HL_HIGHLIGHT_STRINGS (1<<0)
+#define HL_HIGHLIGHT_NUMBERS (1<<1)
+
+/* Here we define an array of syntax highlights by extensions, keywords,
+ * comments delimiters and flags. */
+struct editorSyntax HLDB[] = {
+    {
+        /* C / C++ */
+        C_HL_extensions,
+        C_HL_keywords,
+        "//","/*","*/",
+        HL_HIGHLIGHT_STRINGS | HL_HIGHLIGHT_NUMBERS
+    }
+};
+
+#define HLDB_ENTRIES ((sizeof(HLDB))/(sizeof(HLDB[0])))
+
+
+typedef struct SP SP;
+
+struct SP {
+    union SP_UN {
+        int i;
+        char l;
+    };
+    double d;
+    char c;
+};
+
+typedef struct SP Mem;
+typedef SP Mem2;
+
+
 int main()
 {
-    plan(70);
+    plan(83);
 
     diag("Integer types");
     check_sizes(char, 1);
@@ -197,5 +250,30 @@ int main()
 	is_not_less(sizeof(part2a_erow),  16);
 	is_not_less(sizeof(part3_erow ),  8);
 
+	diag("HLDB");
+	is_not_less(sizeof(HLDB   ), 32);
+	is_not_less(sizeof(HLDB[0]), 32);
+	is_true(sizeof(HLDB) == sizeof(HLDB[0]));
+	is_eq((HLDB_ENTRIES) , 1);
+
+	diag("sqlite examples");
+	is_not_less(sizeof(union SP_UN), 4);
+    Mem m;
+	is_not_less(sizeof(m), 16);
+    is_not_less(sizeof(m), sizeof(double) + sizeof(char));
+    (void)(m);
+    Mem2 m2;
+	is_not_less(sizeof(m2), 16);
+    is_not_less(sizeof(m2), sizeof(double) + sizeof(char));
+    (void)(m2);
+    SP s;
+	is_not_less(sizeof(s), 16);
+    is_not_less(sizeof(s), sizeof(double) + sizeof(char));
+    (void)(s);
+    union SP_UN sp;
+	is_not_less(sizeof(sp), 4);
+    is_not_less(sizeof(sp), sizeof(int));
+    (void)(sp);
+
     done_testing();
 }
diff --git a/tests/struct_sqlite.c b/tests/struct_sqlite.c
deleted file mode 100644
index 2869268..0000000
--- a/tests/struct_sqlite.c
+++ /dev/null
@@ -1,41 +0,0 @@
-#include "tests.h"
-
-typedef struct SP SP;
-
-struct SP {
-    union SP_UN {
-        int i;
-        char l;
-    };
-    double d;
-    char c;
-};
-
-typedef struct SP Mem;
-typedef SP Mem2;
-
-int main()
-{
-    plan(6);
-
-    Mem m;
-    is_true(sizeof(m) >= sizeof(double) + sizeof(char) + sizeof(union SP_UN));
-    (void)(m);
-
-    Mem2 m2;
-    is_true(sizeof(m2) >= sizeof(double) + sizeof(char) + sizeof(union SP_UN));
-    (void)(m2);
-
-    SP s;
-    is_true(sizeof(s) >= sizeof(double) + sizeof(char) + sizeof(union SP_UN));
-    (void)(s);
-
-    union SP_UN sp;
-    is_true(sizeof(sp) >= sizeof(int));
-    (void)(sp);
-
-    is_eq(sizeof(double), 8);
-    is_eq(sizeof(char), 1);
-
-    done_testing();
-}