1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AUTOSAR CountdownAdvent Calendar 2022

Day 10

MISRA C.2.1 Type widening in integer promotion,(wicm3.c ) coding(34)

Last updated at Posted at 2018-03-22

MISRA Cのコード断片をコンパイルして記録した。
https://researchmap.jp/joqmhv6vj-1797580/?lang=japanese

misra-c-2-1-ex-ui-wincm3.c
// 1. filename:	misra-C-2-1-ex-ui-wicm3.c 
//	        misra-C-2-1-ex-ui-wcm2.c
//		misra-C-2-1-ex-ui-wcm.c
// 2. original examples and/or notes: from
// 		(c) MIRA Limited March, 2013 http://www.misra-c.com/
// 		MISRA C:2012
//  	MISRA C.2.1 Type widening in integer promotion, 
//		UI:uint32_t unsigned int, WICM:with initialization,cast, macro and something)
// 3. compile and output mechanism: by 
// 		(c) Ogawa Kiyoshi, January.04, 2014 http ://researchmap.jp/kaizen/MISRA-C/
//		@kaizen_nagoya, kaizen@gifu - u.ac.jp
// 4.compile errors and/or wornings: using
// 4.1.Xcode 5.0/LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn), C90/C99, 32/64 bit
// 		Target: x86_64-apple-darwin11.4.2 //Thread model: posix
//		(c) Apple https://developer.apple.com/xcode/
//			OSX 10.9.1 
// 		(c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.
// 4.2. gcc-4.9 (GCC) 4.9.0 20131229 (experimental) C90/C99/C2011, 32/64 bit, http://gcc.gnu.org/onlinedocs/gcc/Standards.html
//      Copyright (C) 2013 Free Software Foundation, Inc. http://gcc.gnu.org/gcc-4.9/changes.html
// 4.3. Visual Studio Express 2013, most of C99/C11 that is a subset of ISO C++98/C++11.http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2089423-c99-support
//		(c) Microsoft http://www.visualstudio.com/
//			Windows 7, .NET Framework
// http://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/
//			***********************************************************************/
// 			ConsoleApplication1.cpp : 
#include "stdafx.h"
//			If you are not use MS Windows, you may make stdfx.h like this 
// 			#ifndef _WIN32
// 			#define __stdafx_h__
// 			#define _NO_WIN32
// 			// http://social.msdn.microsoft.com/Forums/vstudio/en-US/88339ac0-678d-41d7-a421-519462087277/visual-studio-2008-wont-find-ifdef-win32?forum=vcgenera
// 			#endif //
//		(c) VMware, Inc.
//			VMWare fusion 6
// 4.4 M3T-NC30WA V.5.45 Release 01
//		(c) Renesas Electronics Corporation.http://www.renesas.com/products/mpumcu/m16c/
//			***********************************************************************/
//			*                                                                     */
//			*  FILE        :MISRAC_C_2_1.c                                        */
//			*  DATE        :Tue, Jan 07, 2014                                     */
//			*  	DESCRIPTION :main program file.                                    */
//			*  CPU GROUP   :10                                                    */
//			*                                                                     */
//			*  This file is generated by Renesas Project Generator (Ver.4.19).    */
//			*  NOTE:THIS IS A TYPICAL EXAMPLE.                                    */
// 5. Hardware:  MacBook Pro, 
//		(c) Intel http://ark.intel.com/products/37006/
//		Core 2 Duo 2.53GHz, 8GB, 1067MHz DDR3
// 6. Special Thanks: Upper organizatios and 
// 		NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
//		Toyo Corporation, http://www.toyo.co.jp/English/
//		ISO/IEC JTC1 SC22 WG14, http://www.open-std.org/jtc1/sc22/wg14/www/standards
//		Japan Standard Association, http://bit.ly/1lzykg1
//		NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html
//		Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html
//		WITZ Co.Ltd, http://www.witz-inc.co.jp/products/solution/solution.html
//		SevenWise.co. http://www.7ws.co.jp/index.html
//		TOYOTA Motor Corporation.http://toyota.jp/
#define __MISRA_C_C_2_1_
#include "misra_c.h"
#include <stdio.h>
#include <limits.h>

int main(void){
uint8_t ua = 200, ub =200 ;
uint16_t uc = 40000, ud = 30000;
uint32_t ue	= 2000000000, uf = 2000000000;
uint32_t ug = 2000000000, uh = 3000000000;
uint32_t ui = 4000000000, uj = 5000000000;
uint64_t uk = 9000000000000000000L, ul = 9446700000000000000L;
uint64_t um = 9100000000000000000L, un = 9446700000000000000L;
uint16_t uz=0;	
uint32_t uy=0;
uint64_t uv=0, uw=0, ux=0;
uint128_t uu=0, ut=0;
			PRSV();
	// LLVM 3.2 misra-C-2-1-ui-wc-wm2.c:71:61: error: use of undeclared identifier 'INT_MIN'
	printf("INT_MIN   = %d,     INT_MAX = %d UINT_MAX  = %u\n", INT_MIN, INT_MAX, UINT_MAX);
	printf("LONG_MIN   = %d,     LONG_MAX = %d ULONG_MAX  = %u\n", LONG_MIN, LONG_MAX, ULONG_MAX);
	printf("LLONG_MIN   = %20d,     LLONG_MAX = %20d ULLONG_MAX  = %20u\n", INT_MIN, INT_MAX, UINT_MAX);
	// uint8_t -> uint16_t;
	printf("integer promotion: 8bit\n");
	PRT(u,lu, ua, ub, uz, uint16_t);
	// uint16_t -> uint32_t	
	printf("16bit\n");
PRT(u,lu, uc, ud, uy, uint32_t);
// uint32_t -> uint64_t	
PRT(lu,llu, ue, uf, ux, uint64_t);
	//	cast
	printf("cast: 32bit\n");
PRT(lu,llu, ug, uh, uw, uint64_t);
	// overflow
	printf("truncation\n");
PRT(lu,llu, ui, uj, uv, uint64_t);
	// uint128
	printf("if any 64bit \n");
PRT(lu,llu, uk, ul,uu, uint128_t);

//printf("%" "u" " + %" "u" "= %" "u" ", %" "lu" "\n", ua, ub, ua + ub, uz); uz = ua + ub;printf("%" "u" " + %" "u" "= %" "u" ", %" "lu" "\n", ua, ub, ua + ub, uz); uz = (uint32_t) ua + ub;printf("%" "u"" + %" "u" " = %" "u" ", %" "lu" "\n", ua, ub, (uint32_t) ua + ub, uz);
//printf("%" "u" " + %" "u" "= %" "u" ", %" "lu" "\n", uc, ud, uc + ud, uy); uy = uc + ud;printf("%" "u" " + %" "u" "= %" "u" ", %" "lu" "\n", uc, ud, uc + ud, uy); uy = (uint32_t) uc + ud;printf("%" "u"" + %" "u" " = %" "u" ", %" "lu" "\n", uc, ud, (uint32_t) uc + ud, uy);
//printf("%" "lu" " + %" "lu" "= %" "lu" ", %" "lu" "\n", ue, uf, ue + uf, ux); ux = ue + uf;printf("%" "lu" " + %" "lu" "= %" "lu" ", %" "lu" "\n", ue, uf, ue + uf, ux); ux = (uint64_t) ue + uf;printf("%" "lu"" + %" "lu" " = %" "lu" ", %" "lu" "\n", ue, uf, (uint64_t) ue + uf, ux);
//printf("%" "lu" " + %" "lu" "= %" "lu" ", %" "lu" "\n", ug, uh, ug + uh, uw); uw = ug + uh;printf("%" "lu" " + %" "lu" "= %" "lu" ", %" "lu" "\n", ug, uh, ug + uh, uw); uw = (uint64_t) ug + uh;printf("%" "lu"" + %" "lu" " = %" "lu" ", %" "lu" "\n", ug, uh, (uint64_t) ug + uh, uw);
//printf("%" "lu" " + %" "lu" "= %" "lu" ", %" "lu" "\n", ui, uj, ui + uj, uw); uw = ui + uj;printf("%" "lu" " + %" "lu" "= %" "lu" ", %" "lu" "\n", ui, uj, ui + uj, uw); uw = (uint64_t) ui + uj;printf("%" "lu"" + %" "lu" " = %" "lu" ", %" "lu" "\n", ui, uj, (uint64_t) ui + uj, uw);
//printf("%" "lu" " + %" "lu" "= %" "lu" ", %" "llu" "\n", uk, ul, uk + ul, uv); uv = uk + ul;printf("%" "lu" " + %" "lu" "= %" "lu" ", %" "llu" "\n", uk, ul, uk + ul, uv); uv = (uint128_t) uk + ul;printf("%" "lu"" + %" "lu" " = %" "lu" ", %" "llu" "\n", uk, ul, (uint128_t) uk + ul, uv);
PRT(lu,llu, um, un,ut, uint128_t);
printf("MISRA C.2.1 Type widening in integer promotion\n");
}
// 1, warning
// 1.1 LLVM 3.2
//misra-C-2-1-ui-wc-wm.c:64:33: warning: implicit conversion from 'long' to 'uint32_t' (aka 'unsigned int') changes value from 5000000000 to 705032704 [-Wconstant-conversion]
// uint32_t uh = 4000000000, ui = 5000000000;
//                           ~~   ^~~~~~~~~~
// 1.2 GCC 4.9
//misra-C-2-1-ui-wc-wm.c:61:2: warning: large integer implicitly truncated to unsigned type [-Woverflow]
// uint32_t uh = 4000000000, ui = 5000000000;
//  ^
// 1.3 warning VC/C++ 2013
// c:\misra-c-c-2.1.cpp(128): warning C4305: 
// c:\misra-c-c-2.1.cpp(128): warning C4309: '
//
// 1.4 warning NC30W
//
// 2, output LLVM & GCC4.9
//no ISO/IEC 9899,32bit
//INT_MIN   = -2147483648,     INT_MAX = 2147483647 UINT_MAX  = 4294967295
//LONG_MIN   = 0,     LONG_MAX = -1 ULONG_MAX  = 4294967295
//LLONG_MIN   =          -2147483648,     LLONG_MAX =           2147483647 ULLONG_MAX  =           4294967295
//integer promotion: 8bit
//200 + 200= 400, 0
//200 + 200= 400, 400
//200 + 200 = 400, 400
//16bit
//40000 + 30000= 70000, 0
//40000 + 30000= 70000, 70000
//40000 + 30000 = 70000, 70000
//2000000000 + 2000000000= 4000000000, 0
//2000000000 + 2000000000= 4000000000, 4000000000
//2000000000 + 2000000000 = 4000000000, 4000000000
//cast: 32bit
//2000000000 + 3000000000= 705032704, 0
//2000000000 + 3000000000= 705032704, 705032704
//2000000000 + 3000000000 = 5000000000, 5000000000
//truncation
//4000000000 + 705032704= 410065408, 0
//4000000000 + 705032704= 410065408, 410065408
//4000000000 + 705032704 = 4705032704, 4705032704
//if any 64bit 
//9000000000000000000 + 9446700000000000000= 18446700000000000000, 0
//9000000000000000000 + 9446700000000000000= 18446700000000000000, 18446700000000000000
//9000000000000000000 + 9446700000000000000 = 18446700000000000000, 18446700000000000000
//9100000000000000000 + 9446700000000000000= 99955926290448384, 0
//9100000000000000000 + 9446700000000000000= 99955926290448384, 99955926290448384
//9100000000000000000 + 9446700000000000000 = 99955926290448384, 99955926290448384
//MISRA C.2.1 Type widening in integer promotion
//
// 2.2 vc/c++ 2013
// 2.3 NC30W 
//MISRA C.2.1 Type widening in integer promotion
//INT_MIN = -32768, INT_MAX = 32767 UINT_MAX = 65535
//LONG_MIN = -2147483648, LONG_MAX = 2147483647 ULONG_MAX = 4294967295
//LLONG_MIN = -9223372036854775808, LLONG_MAX = 9223372036854775807 ULLONG_MAX = 18446744073709551615
// 200+200=144
// 200+200=400
// 4000 + 3000 =  4464
// 4000 + 3000 =  70000
// 2000000000+ 2000000000 = 4000000000
// 2000000000+ 2000000000 = 4000000000
// 2000000000+ 3000000000 = 705032704
// 2000000000+ 3000000000  =5000000000
// 4000000000 +705032704  =  410065408
// 4000000000 +705032704  =4705032704
//  9000000000000000000 +9446700000000000000  =  18446700000000000000
// 90000000000000000000 +9446700000000000000  =  18446700000000000000
// 91000000000000000000 +9446700000000000000 =      99955926290448384
// 91000000000000000000 +9446700000000000000 =      99955926290448384

MISRA 用自作ヘッダファイルは

misra_c.h
//--- misra_c.h
// Author, (c) ogawa.kiyoshi, January 7, 2014 
//			@kaizen_nagoya, researchmap.jp/kaizen/MISRA-C/
// Purpose: macro, definition and/or information to Compile Examples.
#ifndef __MISRA_C__
#define __MISRA_C__
#endif

#ifndef  __STANDARD_IO__  // without renesas NC30W
#define __ISO_IEC_9899_1999__
#define __int32bit__
#else // for renesas NC30W only
#define __ISO_IEC_9899_1990__
#define __int16bit__
#endif //__STANDARD_IO__

#ifdef __ISO_IEC_9899_2011__
#include <stdarg.h>
#include <stdint.h> 
#define PRSV() printf("ISO/IEC 9899:2011\n")
#elseif __ISO_IEC_9899_1999__
#include <stdarg.h>
#include <stdint.h> 
#define PRSV() printf("ISO/IEC 9899:1999"\n)
#elseif __ISO_IEC_9899_1990__
// #include <stdint.h> //nc30wa is not supported
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long  uint64_t;
typedef unsigned long  uint128_t;
// macro using printf debug or not.
#define PRSV() printf("ISO/IEC 9899:1990\n")
#elseif __int16bit__
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
typedef unsigned long uint64_t;
typedef unsigned long  uint128_t;
#define PRSV() printf("no ISO/IEC 9899,16bit\n")
#else
#include <stdarg.h>
#include <stdint.h> 
typedef unsigned long long uint128_t;
#define PRSV() printf("no ISO/IEC 9899,32bit\n")
#endif

////////////// for each example
#ifdef __MISRA_C_C_2_1_
#ifndef  __STANDARD_IO__  // without renesas NC30W
#define PR(a,b,c,d,e)  printf("%" #a " + %" #a "= %" #a ", %" #b "\n", c, d, c + d, e)
#define PRC(a,b,c,d,e,f) printf("%" #a" + %" #a " = %" #a ", %" #b "\n", c, d, (f) c + d, e)
#define PRT(a,b,c,d,e,f) PR(a,b, c, d, e); e = c + d;PR(a,b, c, d, e); e = (f) c + d;/*printf(" " #e" =%llu ",e);*/PRC(a,a,c,d,e,f);PRC(a,b,c,d,e,f)
#else // for renesas NC30W only
#define PR(a,b,c,d,e)
#define PRC(a,b,c,d,e,f) 
#endif //__STANDARD_IO__
#endif //__MISRA_C_C_2_1_

//--- stdafx.h not for windows and visual C/C++
// Author, (c) ogawa.kiyoshi, January 7, 2014 
//			@kaizen_nagoya, researchmap.jp/kaizen/MISRA-C/
// Purpose: stdafx.h
#ifndef __stdafx_h__
#define __stdafx_h__
// Note: This is not for MS windows & Visual C/C++
// 			http://social.msdn.microsoft.com/Forums/vstudio/en-US/88339ac0-678d-41d7-a421-519462087277/visual-studio-2008-wont-find-ifdef-win32?forum=vcgenera
#include <stdio.h>
#ifndef _WIN32
#define _NO_WIN32
#else
#error please use MS original stdafx.h
#endif 
#endif

<この項は書きかけです。順次追記します。>
This article is not completed. I will add some words in order.

自己参照

どうやって MISRA Example Suiteをコンパイルするか
https://qiita.com/kaizen_nagoya/items/fbdbff5ff696e2ca7f00

MISRA C.2.1 Type widening in integer promotion,(wicm3.c )
https://qiita.com/kaizen_nagoya/items/6a24db5d51efae358cfb

MISRA-C 2012 Referenceに掲載している文献の入手可能性を確認
https://qiita.com/kaizen_nagoya/items/96dc8b125e462d5575bb

MISRA C まとめ #include
https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9

MISRA C++ 5-0-16
https://qiita.com/kaizen_nagoya/items/7df2d4e05db724752a74

C++ Support(0) 
https://qiita.com/kaizen_nagoya/items/8720d26f762369a80514

Coding Rules(0) C Secure , MISRA and so on
https://qiita.com/kaizen_nagoya/items/400725644a8a0e90fbb0

Ethernet 記事一覧 Ethernet(0)
https://qiita.com/kaizen_nagoya/items/88d35e99f74aefc98794

Wireshark 一覧 wireshark(0)、Ethernet(48)
https://qiita.com/kaizen_nagoya/items/fbed841f61875c4731d0

線網(Wi-Fi)空中線(antenna)(0) 記事一覧(118/300目標)
https://qiita.com/kaizen_nagoya/items/5e5464ac2b24bd4cd001

OSEK OS設計の基礎 OSEK(100)
https://qiita.com/kaizen_nagoya/items/7528a22a14242d2d58a3

Error一覧(C/C++, python, bash...) Error(0)
https://qiita.com/kaizen_nagoya/items/48b6cbc8d68eae2c42b8

なぜdockerで機械学習するか 書籍・ソース一覧作成中 (目標100)
https://qiita.com/kaizen_nagoya/items/ddd12477544bf5ba85e2

言語処理100本ノックをdockerで。python覚えるのに最適。:10+12
https://qiita.com/kaizen_nagoya/items/7e7eb7c543e0c18438c4

プログラムちょい替え(0)一覧:4件
https://qiita.com/kaizen_nagoya/items/296d87ef4bfd516bc394

TOPPERSまとめ #名古屋のIoTは名古屋のOSで
https://qiita.com/kaizen_nagoya/items/9026c049cb0309b9d451

docker(0) 資料集
https://qiita.com/kaizen_nagoya/items/45699eefd62677f69c1d

Qiita-dockerお宝鑑定団
https://qiita.com/kaizen_nagoya/items/509e125263559b5aed5b

The C++ Standard Library: clang++とg++でコンパイルしてみた(まとめ):14件
https://qiita.com/kaizen_nagoya/items/9bdfaa392443d13e5759

C++17 - The Complete Guide clang++とg++でコンパイルしてみた(まとめ):4件
https://qiita.com/kaizen_nagoya/items/c000f307e642990781e1

C++N3242, 2011, ISO/IEC 14882, C++ standard(1) Example code compile list
https://qiita.com/kaizen_nagoya/items/685b5c1a2c17c1bf1318

C++N4606 Working Draft 2016, ISO/IEC 14882, C++ standard(1) Example code compile list
https://qiita.com/kaizen_nagoya/items/df5d62c35bd6ed1c3d43/

C++N4741, 2018 Standard Working Draft on ISO/IEC 14882 sample code compile list
https://qiita.com/kaizen_nagoya/items/3294c014044550896010

C++N4910:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list
https://qiita.com/kaizen_nagoya/items/fc957ddddd402004bb91

Autosar Guidelines C++14 example code compile list(1-169)
https://qiita.com/kaizen_nagoya/items/8ccbf6675c3494d57a76

プログラマによる、プログラマのための、統計と確率のプログラミングとその後 統計と確率一覧(0)
https://qiita.com/kaizen_nagoya/items/6e9897eb641268766909

プログラマが知っていると良い「公序良俗」
https://qiita.com/kaizen_nagoya/items/9fe7c0dfac2fbd77a945

一覧の一覧( The directory of directories of mine.) Qiita(100)
https://qiita.com/kaizen_nagoya/items/7eb0e006543886138f39

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>
This article is an individual impression based on the individual's experience. It has nothing to do with the organization or business to which I currently belong.

文書履歴(document history)

ver. 0.01 初稿  20240520

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?