Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

MISRA C++ 5-0-16

MISRA C++の規則の例題をコンパイル、実行して、何らかの出力をするプログラム。Clang++, G++の出力結果、コンパイルワーニング等は末尾に註釈(comment)

VisualC++の結果も掲載予定。

目次
1 filename: misracpp5-0-16.cpp
2 original examples and/or notes:
3 compile and output mechanism:
4 compile errors and/or warnings:
5. Hardware: MacBook Pro, (Retina, 13-inch, Mid 2014)
6. Special Thanks: Upper organizatios and
7. notes
8. code
9. Error, Warning and/or Output

misracpp5-0-16.cpp
// 1 filename: misracpp5-0-16.cpp
// ver 0.1 18(Mon), Aug, 2017

#include <iostream>
#include <string>
#include <cstdarg>
#include "misracpp.h"

// 2 original examples and/or notes:
// (c) Mira Limited, 2008
// MISRA C++2008, Guidelines for the use of the C++ language in critical systems. June 2008.
// https://www.misra.org.uk/forum/viewforum.php?f=180
std::string ru ("\"6.5 Expressions.\"");
std::string rus ("\"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)\"");

// 3 compile and output mechanism:
// (c) Dr. OGAWA Kiyoshi, kaizen at wh.commufa.jp,
//
// 4 compile errors and/or warnings:
// 4.1  Clang  http://www.llvm.org/
// Copyright (c) 2003-2017, LLVM Project
// clang++ --version
// (c) clang version 5.0.0 (tags/RELEASE_500/final)
// Target: x86_64-apple-darwin14.5.0
// Thread model: posix
// InstalledDir: /usr/local/opt/llvm/bin
// Base standard: https://clang.llvm.org/cxx_status.html
// Command/Options: clang++ -std=c++14 -stdlib=libc++ -Wall misracpp5-0-16.cpp -o misracpp5-0-16L
// Configuration: brew install --with-clang llvm
//
// 4.2. g++ (Homebrew GCC 6.4.0) 6.4.0
// Copyright (C) 2017 Free Software Foundation, Inc.
// This is free software; see the source for copying conditions.
// There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ln -s /usr/local/bin/gcc-6 /usr/local/bin/gcc
// ln -s /usr/local/bin/g++-6 /usr/local/bin/g++
// Base standard: http://gcc.gnu.org/onlinedocs/gcc/Standards.html
// Command/Options: g++  -std=c++14  -Wall misracpp5-0-16.cpp  -o misracpp5-0-16G
// Configuration:brew install gcc6
//
// 5. Hardware:  MacBook Pro,  (Retina, 13-inch, Mid 2014)
// OS X 10.10.5 Yosemite
//Core i5, 2.6GHz, 16GB, 1600MHz DDR3
//(c) Intel http://ark.intel.com/products/series/75024/4th-Generation-Intel-Core-i5-Processors
//
// 6. Special Thanks: Upper organizatios and
// ISO/IEC JTC1 SC22 WG21: http://www.open-std.org/jtc1/sc22/wg21/
// MISRA: https://www.misra.org.uk/forum/index.php
// ISO/IEC JTC1 SC22 WG14: http://www.open-std.org/jtc1/sc22/wg14/
// ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html
// Renesas Electronics Corporation.http://www.renesas.com/
// NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
// Toyo Corporation, http://www.toyo.co.jp/English/
// 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/
// DENSO Corporation, http://www.globaldenso.com/en/
// Aisin Seiki Co. Ltd., http://www.aisin.com/
// Cypress Semiconductor Corp., http://japan.cypress.com/
// Yazaki Corporation, http://www.yazaki-group.com/global/
// Pananosic Corporation, http://www.panasonic.net/
// Denso Create Inc.: http://www.denso-create.jp
// SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp
// CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/
// JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/
// OSC: Open Source Conference, http://www.ospn.jp/

// 7. notes
//{C} Cert C++ text
//{L} LLVM warnings and errors
//{G} Gcc warning and errors
//{S} C++ standard, ISO/IEC JTC1 SC22 WG14
// add 'n' to function and variable names of non compliant example if a same name exists. And sequence number also.
// add 'c' to  function and variable names of compliant examples if a same name exists. And sequence number also.
// 8. code

void f1( const int32_t * a1){
    int32_t a2[10]= {2,4,8,16,32,64,128,256,512,1024};
    const int32_t * p1 = &a1[1];
    int32_t * p2 = &a2[10];
    int32_t * p3 = &a2[11];
    std::cout << *p1 << std::endl << *p2 << std::endl << *p3 << std::endl;

}
    int32_t b=1;
    int32_t c[10]={3,5,7,9,11,13,15,17,19,21};

int main(int argc, char* argv[]){
    f1(&b);
    f1(c);
    std::cout << ru << std::endl << rus << std::endl;
    return argc;
}

// 9. Error, Warning and/or Output
// 9.1 clang
// clang++ -std=c++14 -stdlib=libc++ -Wall misracpp5-0-16.cpp -o misracpp5-0-16L
// 9.1.1 warnings
//misracpp5-0-16.cpp:81:18: warning: array index 11 is past the end of the array
//      (which contains 10 elements) [-Warray-bounds]
//        int32_t * p3 = &a2[11];
//                        ^  ~~
//misracpp5-0-16.cpp:78:2: note: array 'a2' declared here
//        int32_t a2[10]= {2,4,8,16,32,64,128,256,512,1024};
//        ^
//1 warning generated.
// 9.1.2 output
//$ ./misracpp5-0-16L
//0
//-570039104
//1912615492
//5
//-570039104
//1912615492
//"6.5 Expressions."
//"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)"
// 9.2 g++
//g++  -std=c++14  -Wall misracpp5-0-16.cpp  -o misracpp5-0-16G
// 9.2.1 no warning
// 9.2.2 output
//$ ./misracpp5-0-16G
//0
//1586133020
//32767
//5
//1586133020
//32767
//""
//"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)"

Template版print使用

misra5-0-16.cpp
// 1 filename: misracpp5-0-16.cpp
// ver 0.1 18(Mon), Aug, 2017

#include <string>
#include "misracpp.h"

// 2 original examples and/or notes:
// (c) Mira Limited, 2008
// MISRA C++2008, Guidelines for the use of the C++ language in critical systems. June 2008.
// https://www.misra.org.uk/forum/viewforum.php?f=180
string ru ("\"6.5 Expressions.\"");
string rus ("\"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)\"");

// 3 compile and output mechanism:
// (c) Dr. OGAWA Kiyoshi, kaizen at wh.commufa.jp,
//
// 4 compile errors and/or warnings:
// 4.1  Clang  http://www.llvm.org/
// Copyright (c) 2003-2017, LLVM Project
// clang++ --version
// (c) clang version 5.0.0 (tags/RELEASE_500/final)
// Target: x86_64-apple-darwin14.5.0
// Thread model: posix
// InstalledDir: /usr/local/opt/llvm/bin
// Base standard: https://clang.llvm.org/cxx_status.html
// Command/Options: clang++ -std=c++14 -stdlib=libc++ -Wall misracpp5-0-16.cpp -o misracpp5-0-16L
// Configuration: brew install --with-clang llvm
//
// 4.2. g++ (Homebrew GCC 6.4.0) 6.4.0
// Copyright (C) 2017 Free Software Foundation, Inc.
// This is free software; see the source for copying conditions.
// There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ln -s /usr/local/bin/gcc-6 /usr/local/bin/gcc
// ln -s /usr/local/bin/g++-6 /usr/local/bin/g++
// Base standard: http://gcc.gnu.org/onlinedocs/gcc/Standards.html
// Command/Options: g++  -std=c++14  -Wall misracpp5-0-16.cpp  -o misracpp5-0-16G
// Configuration:brew install gcc6
//
// 5. Hardware:  MacBook Pro,  (Retina, 13-inch, Mid 2014)
// OS X 10.10.5 Yosemite
//Core i5, 2.6GHz, 16GB, 1600MHz DDR3
//(c) Intel http://ark.intel.com/products/series/75024/4th-Generation-Intel-Core-i5-Processors
//
// 6. Special Thanks: Upper organizatios and
// ISO/IEC JTC1 SC22 WG21: http://www.open-std.org/jtc1/sc22/wg21/
// MISRA: https://www.misra.org.uk/forum/index.php
// ISO/IEC JTC1 SC22 WG14: http://www.open-std.org/jtc1/sc22/wg14/
// ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html
// Renesas Electronics Corporation.http://www.renesas.com/
// NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
// Toyo Corporation, http://www.toyo.co.jp/English/
// 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/
// DENSO Corporation, http://www.globaldenso.com/en/
// Aisin Seiki Co. Ltd., http://www.aisin.com/
// Cypress Semiconductor Corp., http://japan.cypress.com/
// Yazaki Corporation, http://www.yazaki-group.com/global/
// Pananosic Corporation, http://www.panasonic.net/
// Denso Create Inc.: http://www.denso-create.jp
// SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp
// CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/
// JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/
// OSC: Open Source Conference, http://www.ospn.jp/

// 7. notes
//{C} Cert C++ text
//{L} LLVM warnings and errors
//{G} Gcc warning and errors
//{S} C++ standard, ISO/IEC JTC1 SC22 WG14
// add 'n' to function and variable names of non compliant example if a same name exists. And sequence number also.
// add 'c' to  function and variable names of compliant examples if a same name exists. And sequence number also.
// 8. code

void f1( const int32_t * a1){
    int32_t a2[10]= {2,4,8,16,32,64,128,256,512,1024};
    const int32_t * p1 = &a1[1];
    int32_t * p2 = &a2[10];
    int32_t * p3 = &a2[11];
    print(*p1, *p2, *p3);
}
    int32_t b=1;
    int32_t c[10]={3,5,7,9,11,13,15,17,19,21};

int main(){
    f1(&b);
    f1(c);
    print(ru, rus);
    return EXIT_SUCCESS;
}

// 9. Error, Warning and/or Output
// 9.1 clang
// clang++ -std=c++14 -stdlib=libc++ -Wall misracpp5-0-16.cpp -o misracpp5-0-16L
// 9.1.1 warnings
//misracpp5-0-16.cpp:81:18: warning: array index 11 is past the end of the array
//      (which contains 10 elements) [-Warray-bounds]
//        int32_t * p3 = &a2[11];
//                        ^  ~~
//misracpp5-0-16.cpp:78:2: note: array 'a2' declared here
//        int32_t a2[10]= {2,4,8,16,32,64,128,256,512,1024};
//        ^
//1 warning generated.
// 9.1.2 output
//$ ./misracpp5-0-16L
//0
//-570039104
//1912615492
//5
//-570039104
//1912615492
//"6.5 Expressions."
//"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)"
// 9.2 g++
//g++  -std=c++14  -Wall misracpp5-0-16.cpp  -o misracpp5-0-16G
// 9.2.1 no warning
// 9.2.2 output
//$ ./misracpp5-0-16G
//0
//1586133020
//32767
//5
//1586133020
//32767
//""
//"Rule 5-0-16 (Required) A pointer operand and any pointer resulting from pointer arithmetic using that operand shall both address elements of the same array.[Undefined 5.7(5)"

misracpp.h

misracpp.h
#ifndef MISRA
#define MISRA


#include <iostream>
#include <utility>
#include <cstdlib>

using namespace std;

// パラメータパックが空になったら終了
void print() {}

// ひとつ以上のパラメータを受け取るようにし、
// 可変引数を先頭とそれ以外に分割する
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail)
{
  cout << head << endl;

  // パラメータパックtailをさらにheadとtailに分割する
  print(std::move(tail)...);
}
#endif

関連文献

どうやって 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

文書履歴(document history)

ver. 0.10 初稿 20180202
ver. 0.11 template版print 20180807

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?