Skip to content

Commit a45afce

Browse files
authored
added @throws to documentation / adjusted some catch handlers (#600)
1 parent e177522 commit a45afce

File tree

2 files changed

+66
-34
lines changed

2 files changed

+66
-34
lines changed

simplecpp.cpp

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <cstdlib>
3434
#include <cstring>
3535
#include <ctime>
36-
#include <exception>
3736
#include <fstream>
3837
#include <iostream>
3938
#include <istream>
@@ -414,6 +413,9 @@ class StdCharBufStream : public simplecpp::TokenList::Stream {
414413

415414
class FileStream : public simplecpp::TokenList::Stream {
416415
public:
416+
/**
417+
* @throws simplecpp::Output thrown if file is not found
418+
*/
417419
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
418420
explicit FileStream(const std::string &filename, std::vector<std::string> &files)
419421
: file(fopen(filename.c_str(), "rb"))
@@ -487,7 +489,7 @@ simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::st
487489
try {
488490
FileStream stream(filename, filenames);
489491
readfile(stream,filename,outputList);
490-
} catch (const simplecpp::Output & e) { // TODO handle extra type of errors
492+
} catch (const simplecpp::Output & e) {
491493
outputList->push_back(e);
492494
}
493495
}
@@ -1488,6 +1490,9 @@ namespace simplecpp {
14881490
public:
14891491
explicit Macro(std::vector<std::string> &f) : nameTokDef(nullptr), valueToken(nullptr), endToken(nullptr), files(f), tokenListDefine(f), variadic(false), variadicOpt(false), valueDefinedInCode_(false) {}
14901492

1493+
/**
1494+
* @throws std::runtime_error thrown on bad macro syntax
1495+
*/
14911496
Macro(const Token *tok, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(true) {
14921497
if (sameline(tok->previousSkipComments(), tok))
14931498
throw std::runtime_error("bad macro syntax");
@@ -1504,6 +1509,9 @@ namespace simplecpp {
15041509
throw std::runtime_error("bad macro syntax");
15051510
}
15061511

1512+
/**
1513+
* @throws std::runtime_error thrown on bad macro syntax
1514+
*/
15071515
Macro(const std::string &name, const std::string &value, std::vector<std::string> &f) : nameTokDef(nullptr), files(f), tokenListDefine(f), valueDefinedInCode_(false) {
15081516
const std::string def(name + ' ' + value);
15091517
StdCharBufStream stream(reinterpret_cast<const unsigned char*>(def.data()), def.size());
@@ -1552,7 +1560,9 @@ namespace simplecpp {
15521560
* @param macros list of macros
15531561
* @param inputFiles the input files
15541562
* @return token after macro
1555-
* @throw Can throw wrongNumberOfParameters or invalidHashHash
1563+
* @throws Error thrown on missing or invalid preprocessor directives
1564+
* @throws wrongNumberOfParameters thrown on invalid number of parameters
1565+
* @throws invalidHashHash thrown on invalid ## usage
15561566
*/
15571567
const Token * expand(TokenList & output,
15581568
const Token * rawtok,
@@ -2544,7 +2554,9 @@ namespace simplecpp {
25442554
}
25452555
}
25462556

2547-
/** Evaluate sizeof(type) */
2557+
/** Evaluate sizeof(type)
2558+
* @throws std::runtime_error thrown on missing arguments or invalid expression
2559+
*/
25482560
static void simplifySizeof(simplecpp::TokenList &expr, const std::map<std::string, std::size_t> &sizeOfType)
25492561
{
25502562
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
@@ -2612,6 +2624,10 @@ static std::string dirPath(const std::string& path, bool withTrailingSlash=true)
26122624
}
26132625

26142626
static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
2627+
2628+
/** Evaluate __has_include(include)
2629+
* @throws std::runtime_error thrown on missing arguments or invalid expression
2630+
*/
26152631
static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI &dui)
26162632
{
26172633
if (!isCpp17OrLater(dui) && !isGnu(dui))
@@ -2668,6 +2684,9 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI
26682684
}
26692685
}
26702686

2687+
/** Evaluate name
2688+
* @throws std::runtime_error thrown on undefined function-like macro
2689+
*/
26712690
static void simplifyName(simplecpp::TokenList &expr)
26722691
{
26732692
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
@@ -2696,7 +2715,7 @@ static void simplifyName(simplecpp::TokenList &expr)
26962715
* unsigned long long value, updating pos to point to the first
26972716
* unused element of s.
26982717
* Returns ULLONG_MAX if the result is not representable and
2699-
* throws if the above requirements were not possible to satisfy.
2718+
* @throws std::runtime_error thrown if the above requirements were not possible to satisfy.
27002719
*/
27012720
static unsigned long long stringToULLbounded(
27022721
const std::string& s,
@@ -2716,34 +2735,6 @@ static unsigned long long stringToULLbounded(
27162735
return value;
27172736
}
27182737

2719-
/* Converts character literal (including prefix, but not ud-suffix)
2720-
* to long long value.
2721-
*
2722-
* Assumes ASCII-compatible single-byte encoded str for narrow literals
2723-
* and UTF-8 otherwise.
2724-
*
2725-
* For target assumes
2726-
* - execution character set encoding matching str
2727-
* - UTF-32 execution wide-character set encoding
2728-
* - requirements for __STDC_UTF_16__, __STDC_UTF_32__ and __STDC_ISO_10646__ satisfied
2729-
* - char16_t is 16bit wide
2730-
* - char32_t is 32bit wide
2731-
* - wchar_t is 32bit wide and unsigned
2732-
* - matching char signedness to host
2733-
* - matching sizeof(int) to host
2734-
*
2735-
* For host assumes
2736-
* - ASCII-compatible execution character set
2737-
*
2738-
* For host and target assumes
2739-
* - CHAR_BIT == 8
2740-
* - two's complement
2741-
*
2742-
* Implements multi-character narrow literals according to GCC's behavior,
2743-
* except multi code unit universal character names are not supported.
2744-
* Multi-character wide literals are not supported.
2745-
* Limited support of universal character names for non-UTF-8 execution character set encodings.
2746-
*/
27472738
long long simplecpp::characterLiteralToLL(const std::string& str)
27482739
{
27492740
// default is wide/utf32
@@ -2937,6 +2928,9 @@ long long simplecpp::characterLiteralToLL(const std::string& str)
29372928
return multivalue;
29382929
}
29392930

2931+
/**
2932+
* @throws std::runtime_error thrown on invalid literal
2933+
*/
29402934
static void simplifyNumbers(simplecpp::TokenList &expr)
29412935
{
29422936
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
@@ -2959,6 +2953,10 @@ static void simplifyComments(simplecpp::TokenList &expr)
29592953
}
29602954
}
29612955

2956+
/**
2957+
* @throws std::runtime_error thrown on invalid literals, missing sizeof arguments or invalid expressions,
2958+
* missing __has_include() arguments or expressions, undefined function-like macros, invalid number literals
2959+
*/
29622960
static long long evaluate(simplecpp::TokenList &expr, const simplecpp::DUI &dui, const std::map<std::string, std::size_t> &sizeOfType)
29632961
{
29642962
simplifyComments(expr);
@@ -3692,7 +3690,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36923690
const long long result = evaluate(expr, dui, sizeOfType);
36933691
conditionIsTrue = (result != 0);
36943692
}
3695-
} catch (const std::exception &e) {
3693+
} catch (const std::runtime_error &e) {
36963694
if (outputList) {
36973695
std::string msg = "failed to evaluate " + std::string(rawtok->str() == IF ? "#if" : "#elif") + " condition";
36983696
if (e.what() && *e.what())

simplecpp.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,18 @@ namespace simplecpp {
338338
void combineOperators();
339339

340340
void constFoldUnaryNotPosNeg(Token *tok);
341+
/**
342+
* @throws std::overflow_error thrown on overflow or division by zero
343+
*/
341344
void constFoldMulDivRem(Token *tok);
342345
void constFoldAddSub(Token *tok);
343346
void constFoldShift(Token *tok);
344347
void constFoldComparison(Token *tok);
345348
void constFoldBitwise(Token *tok);
346349
void constFoldLogicalOp(Token *tok);
350+
/**
351+
* @throws std::runtime_error thrown on invalid expressions
352+
*/
347353
void constFoldQuestionOp(Token *&tok1);
348354

349355
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
@@ -501,6 +507,34 @@ namespace simplecpp {
501507
id_map_type mIdMap;
502508
};
503509

510+
/** Converts character literal (including prefix, but not ud-suffix) to long long value.
511+
*
512+
* Assumes ASCII-compatible single-byte encoded str for narrow literals
513+
* and UTF-8 otherwise.
514+
*
515+
* For target assumes
516+
* - execution character set encoding matching str
517+
* - UTF-32 execution wide-character set encoding
518+
* - requirements for __STDC_UTF_16__, __STDC_UTF_32__ and __STDC_ISO_10646__ satisfied
519+
* - char16_t is 16bit wide
520+
* - char32_t is 32bit wide
521+
* - wchar_t is 32bit wide and unsigned
522+
* - matching char signedness to host
523+
* - matching sizeof(int) to host
524+
*
525+
* For host assumes
526+
* - ASCII-compatible execution character set
527+
*
528+
* For host and target assumes
529+
* - CHAR_BIT == 8
530+
* - two's complement
531+
*
532+
* Implements multi-character narrow literals according to GCC's behavior,
533+
* except multi code unit universal character names are not supported.
534+
* Multi-character wide literals are not supported.
535+
* Limited support of universal character names for non-UTF-8 execution character set encodings.
536+
* @throws std::runtime_error thrown on invalid literal
537+
*/
504538
SIMPLECPP_LIB long long characterLiteralToLL(const std::string& str);
505539

506540
SIMPLECPP_LIB FileDataCache load(const TokenList &rawtokens, std::vector<std::string> &filenames, const DUI &dui, OutputList *outputList = nullptr, FileDataCache cache = {});

0 commit comments

Comments
 (0)