From 8a8c1fc82f5b1f7e5f7d82970e39d6064f2fd0b4 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Thu, 1 Jan 2015 09:29:28 +0200
Subject: [PATCH] py: Add basic framework for issuing compile/runtime warnings.

---
 py/builtinimport.c  |  2 +-
 py/emit.h           |  6 +++++
 py/emitnative.c     |  1 +
 py/mpconfig.h       |  5 +++++
 py/py.mk            |  1 +
 py/runtime.h        |  6 +++++
 py/warning.c        | 54 +++++++++++++++++++++++++++++++++++++++++++++
 unix/mpconfigport.h |  1 +
 8 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 py/warning.c

diff --git a/py/builtinimport.c b/py/builtinimport.c
index a8f6a8174..fd804ee35 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -323,7 +323,7 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
                     vstr_add_str(&path, "__init__.py");
                     if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
                         vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
-                        printf("Notice: %s is imported as namespace package\n", vstr_str(&path));
+                        mp_warning("%s is imported as namespace package", vstr_str(&path));
                     } else {
                         do_load(module_obj, &path);
                         vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
diff --git a/py/emit.h b/py/emit.h
index 927e33ea7..f039770a5 100644
--- a/py/emit.h
+++ b/py/emit.h
@@ -204,4 +204,10 @@ extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table;
 emit_inline_asm_t *emit_inline_thumb_new(mp_uint_t max_num_labels);
 void emit_inline_thumb_free(emit_inline_asm_t *emit);
 
+#if MICROPY_WARNINGS
+void mp_emitter_warning(pass_kind_t pass, const char *msg);
+#else
+#define mp_emitter_warning(pass, msg)
+#endif
+
 #endif // __MICROPY_INCLUDED_PY_EMIT_H__
diff --git a/py/emitnative.c b/py/emitnative.c
index c2fe951be..556a977a0 100644
--- a/py/emitnative.c
+++ b/py/emitnative.c
@@ -1636,6 +1636,7 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
 STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
     // TODO implement me!
     // could support for Python types, just set to None (so GC can reclaim it)
+    mp_emitter_warning(emit->pass, "Native codegeneration doesn't support deleting local");
 }
 
 STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
diff --git a/py/mpconfig.h b/py/mpconfig.h
index b193051c5..bbc791c0b 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -251,6 +251,11 @@ typedef long long mp_longint_impl_t;
 #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)
 #endif
 
+// Whether issue warnings during compiling/execution
+#ifndef MICROPY_WARNINGS
+#define MICROPY_WARNINGS (0)
+#endif
+
 // Float and complex implementation
 #define MICROPY_FLOAT_IMPL_NONE (0)
 #define MICROPY_FLOAT_IMPL_FLOAT (1)
diff --git a/py/py.mk b/py/py.mk
index f54c06cbc..4c4094eb6 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -51,6 +51,7 @@ PY_O_BASENAME = \
 	nativeglue.o \
 	stackctrl.o \
 	argcheck.o \
+	warning.o \
 	map.o \
 	obj.o \
 	objarray.o \
diff --git a/py/runtime.h b/py/runtime.h
index 59d61e708..711b15f0d 100644
--- a/py/runtime.h
+++ b/py/runtime.h
@@ -126,4 +126,10 @@ extern struct _mp_obj_list_t mp_sys_argv_obj;
 #define mp_sys_path ((mp_obj_t)&mp_sys_path_obj)
 #define mp_sys_argv ((mp_obj_t)&mp_sys_argv_obj)
 
+#if MICROPY_WARNINGS
+void mp_warning(const char *msg, ...);
+#else
+#define mp_warning(msg, ...)
+#endif
+
 #endif // __MICROPY_INCLUDED_PY_RUNTIME_H__
diff --git a/py/warning.c b/py/warning.c
new file mode 100644
index 000000000..c7461c0f5
--- /dev/null
+++ b/py/warning.c
@@ -0,0 +1,54 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include <stdint.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "mpconfig.h"
+#include "misc.h"
+#include "qstr.h"
+#include "obj.h"
+#include "compile.h"
+#include "scope.h"
+#include "emit.h"
+
+#if MICROPY_WARNINGS
+
+void mp_warning(const char *msg, ...) {
+    va_list args;
+    va_start(args, msg);
+    printf("Warning: ");
+    vprintf(msg, args);
+    printf("\n");
+}
+
+void mp_emitter_warning(pass_kind_t pass, const char *msg) {
+    if (pass == MP_PASS_CODE_SIZE) {
+        mp_warning(msg, NULL);
+    }
+}
+
+#endif // MICROPY_WARNINGS
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index a895aeab0..fc2a18278 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -74,6 +74,7 @@
 // Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc.
 // names in exception messages (may require more RAM).
 #define MICROPY_ERROR_REPORTING     (MICROPY_ERROR_REPORTING_DETAILED)
+#define MICROPY_WARNINGS            (1)
 
 // Define to 1 to use undertested inefficient GC helper implementation
 // (if more efficient arch-specific one is not available).
-- 
GitLab