py: Add basic framework for issuing compile/runtime warnings.

This commit is contained in:
Paul Sokolovsky 2015-01-01 09:29:28 +02:00
parent ebde3c694f
commit 8a8c1fc82f
8 changed files with 75 additions and 1 deletions

View File

@ -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

View File

@ -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__

View File

@ -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) {

View File

@ -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)

View File

@ -51,6 +51,7 @@ PY_O_BASENAME = \
nativeglue.o \
stackctrl.o \
argcheck.o \
warning.o \
map.o \
obj.o \
objarray.o \

View File

@ -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__

54
py/warning.c Normal file
View File

@ -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

View File

@ -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).